

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>quippy.oo_fortran &mdash; quippy 7ea04c5+ documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../_static/favicon.ico"/>
  
  
  

  
  <script type="text/javascript" src="../../_static/js/modernizr.min.js"></script>
  
    
      <script type="text/javascript" id="documentation_options" data-url_root="../../" src="../../_static/documentation_options.js"></script>
        <script type="text/javascript" src="../../_static/jquery.js"></script>
        <script type="text/javascript" src="../../_static/underscore.js"></script>
        <script type="text/javascript" src="../../_static/doctools.js"></script>
        <script type="text/javascript" src="../../_static/language_data.js"></script>
        <script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
        <script type="text/x-mathjax-config">MathJax.Hub.Config({"tex2jax": {"processClass": "math|output_area", "processEscapes": true, "ignoreClass": "document", "inlineMath": [["$", "$"], ["\\(", "\\)"]]}})</script>
    
    <script type="text/javascript" src="../../_static/js/theme.js"></script>

    

  
  <link rel="stylesheet" href="../../_static/css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />
  <link rel="stylesheet" href="../../_static/theme_overrides.css" type="text/css" />
    <link rel="index" title="Index" href="../../genindex.html" />
    <link rel="search" title="Search" href="../../search.html" /> 
</head>

<body class="wy-body-for-nav">

   
  <div class="wy-grid-for-nav">
    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search" >
          

          
            <a href="../../index.html" class="icon icon-home"> quippy
          

          
            
            <img src="../../_static/hybrid.png" class="logo" alt="Logo"/>
          
          </a>

          
            
            
              <div class="version">
                7ea04c5+
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
    
            
            
              
            
            
              <ul>
<li class="toctree-l1"><a class="reference internal" href="../../intro.html">Introduction to <code class="docutils literal notranslate"><span class="pre">quippy</span></code></a></li>
<li class="toctree-l1"><a class="reference internal" href="../../install.html">Installation of QUIP and quippy</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../Tutorials/index.html">Tutorials</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../quippy.html">Quippy library reference</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../visualisation.html">Visualisation Tools</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../fortran_wrapper.html">Appendix: wrapping Fortran 90 Code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../teach_sparse.html">Command line options of the teach_sparse main program</a></li>
</ul>

            
          
    <p></p>
    <ul>
        <li><a href="../../genindex.html">Index</a></li>
        <li><a href="../../py-modindex.html">Module Index</a></li>
        <li><a href="../../search.html">Search Page</a></li>
        <li><a href="../index.html">Module Source Code</a></li>
    </ul>
  
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../index.html">quippy</a>
        
      </nav>


      <div class="wy-nav-content">
        
        <div class="rst-content">
        
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../index.html">Module code</a> &raquo;</li>
        
      <li>quippy.oo_fortran</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for quippy.oo_fortran</h1><div class="highlight"><pre>
<span></span><span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   quippy: Python interface to QUIP atomistic simulation library</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   Copyright James Kermode 2010</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   These portions of the source code are released under the GNU General</span>
<span class="c1"># HQ X   Public License, version 2, http://www.gnu.org/copyleft/gpl.html</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   If you would like to license the source code under different terms,</span>
<span class="c1"># HQ X   please contact James Kermode, james.kermode@gmail.com</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   When using this software, please cite the following reference:</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ X   http://www.jrkermode.co.uk/quippy</span>
<span class="c1"># HQ X</span>
<span class="c1"># HQ XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX</span>

<span class="sd">&quot;&quot;&quot;Object oriented interface on top of f2py generated wrappers.</span>

<span class="sd">This module adds support for derived types to f2py generated modules,</span>
<span class="sd">providing the Fortran source code was built using</span>
<span class="sd">:mod:`f2py_wrapper_gen`.&quot;&quot;&quot;</span>

<span class="kn">import</span> <span class="nn">logging</span>
<span class="kn">import</span> <span class="nn">inspect</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">imp</span>
<span class="kn">import</span> <span class="nn">weakref</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">from</span> <span class="nn">types</span> <span class="k">import</span> <span class="n">MethodType</span>

<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>

<span class="kn">from</span> <span class="nn">quippy.farray</span> <span class="k">import</span> <span class="o">*</span>
<span class="kn">from</span> <span class="nn">quippy.util</span> <span class="k">import</span> <span class="n">args_str</span><span class="p">,</span> <span class="n">is_interactive_shell</span>
<span class="kn">from</span> <span class="nn">quippy.dictmixin</span> <span class="k">import</span> <span class="n">PuPyDictionary</span>
<span class="kn">from</span> <span class="nn">quippy</span> <span class="k">import</span> <span class="n">get_fortran_indexing</span>
<span class="kn">import</span> <span class="nn">quippy.arraydata</span> <span class="k">as</span> <span class="nn">arraydata</span>

<span class="n">__all__</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;FortranDerivedType&#39;</span><span class="p">,</span> <span class="s1">&#39;FortranDerivedTypes&#39;</span><span class="p">,</span> <span class="s1">&#39;wrap_all&#39;</span><span class="p">]</span>

<span class="n">major</span><span class="p">,</span> <span class="n">minor</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">version_info</span><span class="p">[</span><span class="mi">0</span><span class="p">:</span><span class="mi">2</span><span class="p">]</span>

<span class="k">if</span> <span class="p">(</span><span class="n">major</span><span class="p">,</span> <span class="n">minor</span><span class="p">)</span> <span class="o">&lt;</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">5</span><span class="p">):</span>
    <span class="nb">all</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">seq</span><span class="p">:</span> <span class="ow">not</span> <span class="kc">False</span> <span class="ow">in</span> <span class="n">seq</span>
    <span class="nb">any</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">seq</span><span class="p">:</span> <span class="kc">True</span> <span class="ow">in</span> <span class="n">seq</span>

<span class="n">wraplog</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s1">&#39;quippy.oo_fortran&#39;</span><span class="p">)</span>

<span class="n">fortran_class_prefix</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

<span class="n">py_keywords</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;and&#39;</span><span class="p">,</span>       <span class="s1">&#39;del&#39;</span><span class="p">,</span>       <span class="s1">&#39;from&#39;</span><span class="p">,</span>      <span class="s1">&#39;not&#39;</span><span class="p">,</span>       <span class="s1">&#39;while&#39;</span><span class="p">,</span>
               <span class="s1">&#39;as&#39;</span><span class="p">,</span>        <span class="s1">&#39;elif&#39;</span><span class="p">,</span>      <span class="s1">&#39;global&#39;</span><span class="p">,</span>    <span class="s1">&#39;or&#39;</span><span class="p">,</span>        <span class="s1">&#39;with&#39;</span><span class="p">,</span>
               <span class="s1">&#39;assert&#39;</span><span class="p">,</span>    <span class="s1">&#39;else&#39;</span><span class="p">,</span>      <span class="s1">&#39;if&#39;</span><span class="p">,</span>        <span class="s1">&#39;pass&#39;</span><span class="p">,</span>      <span class="s1">&#39;yield&#39;</span><span class="p">,</span>
               <span class="s1">&#39;break&#39;</span><span class="p">,</span>     <span class="s1">&#39;except&#39;</span><span class="p">,</span>    <span class="s1">&#39;import&#39;</span><span class="p">,</span>    <span class="s1">&#39;print&#39;</span><span class="p">,</span>
               <span class="s1">&#39;class&#39;</span><span class="p">,</span>     <span class="s1">&#39;exec&#39;</span><span class="p">,</span>      <span class="s1">&#39;in&#39;</span><span class="p">,</span>        <span class="s1">&#39;raise&#39;</span><span class="p">,</span>
               <span class="s1">&#39;continue&#39;</span><span class="p">,</span>  <span class="s1">&#39;finally&#39;</span><span class="p">,</span>   <span class="s1">&#39;is&#39;</span><span class="p">,</span>        <span class="s1">&#39;return&#39;</span><span class="p">,</span>
               <span class="s1">&#39;def&#39;</span><span class="p">,</span>       <span class="s1">&#39;for&#39;</span><span class="p">,</span>       <span class="s1">&#39;lambda&#39;</span><span class="p">,</span>    <span class="s1">&#39;try&#39;</span><span class="p">]</span>

<span class="n">py_keywords_map</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="n">k</span><span class="p">,</span><span class="n">k</span><span class="o">+</span><span class="s1">&#39;_&#39;</span><span class="p">)</span> <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="n">py_keywords</span><span class="p">])</span>

<span class="n">numpy_to_fortran</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;d&#39;</span><span class="p">:</span> <span class="s1">&#39;real(dp)&#39;</span><span class="p">,</span>
    <span class="s1">&#39;i&#39;</span><span class="p">:</span> <span class="s1">&#39;integer&#39;</span><span class="p">,</span>
    <span class="s1">&#39;b&#39;</span><span class="p">:</span> <span class="s1">&#39;logical&#39;</span><span class="p">,</span>
    <span class="s1">&#39;c&#39;</span><span class="p">:</span> <span class="s1">&#39;complex(dp)&#39;</span><span class="p">,</span>
    <span class="s1">&#39;S&#39;</span><span class="p">:</span> <span class="s1">&#39;character&#39;</span><span class="p">,</span>
    <span class="s1">&#39;f&#39;</span><span class="p">:</span> <span class="s1">&#39;real(dp)&#39;</span>
    <span class="p">}</span>

<span class="n">FortranDerivedTypes</span> <span class="o">=</span> <span class="p">{}</span>
<span class="n">FortranRoutines</span> <span class="o">=</span> <span class="p">{}</span>

<span class="kn">from</span> <span class="nn">quippy</span> <span class="k">import</span> <span class="n">QUIPPY_TRUE</span><span class="p">,</span> <span class="n">QUIPPY_FALSE</span>

<span class="kn">from</span> <span class="nn">numpy.f2py.crackfortran</span> <span class="k">import</span> <span class="n">badnames</span>

<span class="n">special_names</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s1">&#39;assignment(=)&#39;</span> <span class="p">:</span> <span class="s1">&#39;assignment&#39;</span><span class="p">,</span>
    <span class="p">}</span>

<span class="n">rev_special_names</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">v</span><span class="p">,</span><span class="n">k</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">special_names</span><span class="o">.</span><span class="n">iteritems</span><span class="p">())</span>


<span class="k">def</span> <span class="nf">is_scalar_type</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="k">if</span> <span class="n">t</span> <span class="ow">in</span> <span class="n">numpy_to_fortran</span><span class="o">.</span><span class="n">values</span><span class="p">():</span> <span class="k">return</span> <span class="kc">True</span>
    <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;character&#39;</span><span class="p">):</span> <span class="k">return</span> <span class="kc">True</span>
    <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;integer&#39;</span><span class="p">):</span> <span class="k">return</span> <span class="kc">True</span>
    <span class="k">return</span> <span class="kc">False</span>

<span class="k">def</span> <span class="nf">fortran_equivalent_type</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="n">tmap</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;int&#39;</span><span class="p">:</span> <span class="s1">&#39;integer&#39;</span><span class="p">,</span>
            <span class="s1">&#39;float&#39;</span><span class="p">:</span> <span class="s1">&#39;real(dp)&#39;</span><span class="p">,</span>
            <span class="s1">&#39;bool&#39;</span><span class="p">:</span> <span class="s1">&#39;logical&#39;</span><span class="p">,</span>
            <span class="s1">&#39;str&#39;</span><span class="p">:</span> <span class="s1">&#39;character&#39;</span><span class="p">,</span>
            <span class="s1">&#39;complex&#39;</span> <span class="p">:</span> <span class="s1">&#39;complex(dp)&#39;</span>
            <span class="p">}</span>

    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span> <span class="ow">in</span> <span class="n">tmap</span><span class="p">:</span>
        <span class="k">return</span> <span class="p">(</span><span class="n">tmap</span><span class="p">[</span><span class="nb">type</span><span class="p">(</span><span class="n">t</span><span class="p">)</span><span class="o">.</span><span class="vm">__name__</span><span class="p">],</span> <span class="s1">&#39;scalar&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="n">FortranDerivedType</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">typename</span><span class="p">,</span><span class="bp">cls</span> <span class="ow">in</span> <span class="n">FortranDerivedTypes</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">t</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
                <span class="k">return</span> <span class="p">(</span><span class="n">typename</span><span class="p">,</span> <span class="s1">&#39;scalar&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Unknown derived type </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">t</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="s1">&#39;__iter__&#39;</span><span class="p">)</span> <span class="ow">or</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="s1">&#39;dtype&#39;</span><span class="p">):</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">array</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">kind</span> <span class="ow">in</span> <span class="n">numpy_to_fortran</span><span class="p">:</span>
            <span class="k">return</span> <span class="p">(</span><span class="n">numpy_to_fortran</span><span class="p">[</span><span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">kind</span><span class="p">],</span> <span class="n">a</span><span class="o">.</span><span class="n">shape</span><span class="p">,</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">itemsize</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Unknown array type </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">kind</span><span class="p">)</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Unknown type </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">type</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>

<span class="k">def</span> <span class="nf">python_equivalent_type</span><span class="p">(</span><span class="n">t</span><span class="p">):</span>
    <span class="n">tmap</span> <span class="o">=</span> <span class="p">{</span> <span class="s1">&#39;integer&#39;</span><span class="p">:</span> <span class="s1">&#39;int&#39;</span><span class="p">,</span>
             <span class="s1">&#39;real(dp)&#39;</span><span class="p">:</span> <span class="s1">&#39;float&#39;</span><span class="p">,</span>
             <span class="s1">&#39;logical&#39;</span><span class="p">:</span> <span class="s1">&#39;bool&#39;</span><span class="p">,</span>
             <span class="s1">&#39;character&#39;</span><span class="p">:</span> <span class="s1">&#39;str&#39;</span><span class="p">,</span>
             <span class="s1">&#39;complex(dp)&#39;</span> <span class="p">:</span> <span class="s1">&#39;complex&#39;</span><span class="p">,</span>
             <span class="s1">&#39;&#39;</span> <span class="p">:</span> <span class="s1">&#39;None&#39;</span><span class="p">}</span>
    <span class="n">t</span> <span class="o">=</span> <span class="n">tmap</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">t</span><span class="p">,</span><span class="n">t</span><span class="p">)</span>
    <span class="k">if</span> <span class="n">t</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;character(&#39;</span><span class="p">):</span>
        <span class="n">t</span> <span class="o">=</span> <span class="s1">&#39;str&#39;</span>
    <span class="k">return</span> <span class="n">t</span>


<span class="k">def</span> <span class="nf">type_is_compatible</span><span class="p">(</span><span class="n">spec</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
    <span class="k">if</span> <span class="s1">&#39;optional&#39;</span> <span class="ow">in</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="ow">and</span> <span class="n">arg</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="n">arg_type</span><span class="p">,</span> <span class="n">dims</span><span class="p">,</span> <span class="n">itemsize</span> <span class="o">=</span> <span class="n">fortran_equivalent_type</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">TypeError</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">False</span>

    <span class="k">if</span> <span class="n">dims</span> <span class="o">==</span> <span class="p">():</span> <span class="n">dims</span> <span class="o">=</span> <span class="s1">&#39;scalar&#39;</span>

    <span class="n">spec_type</span> <span class="o">=</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
    <span class="k">if</span> <span class="n">spec_type</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;character&#39;</span><span class="p">):</span>
        <span class="n">spec_type</span> <span class="o">=</span> <span class="s1">&#39;character&#39;</span>

    <span class="c1"># First check type matches</span>
    <span class="k">if</span> <span class="n">arg_type</span> <span class="o">!=</span> <span class="n">spec_type</span><span class="p">:</span> <span class="k">return</span> <span class="kc">False</span>

    <span class="c1"># Now check dimensions</span>
    <span class="n">dimattrs</span> <span class="o">=</span> <span class="p">[</span><span class="n">s</span> <span class="k">for</span> <span class="n">s</span> <span class="ow">in</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="n">s</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;dimension&#39;</span><span class="p">)]</span>

    <span class="k">if</span> <span class="n">dims</span> <span class="o">==</span> <span class="s1">&#39;scalar&#39;</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">dimattrs</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="p">(</span><span class="n">dimattr</span><span class="p">,)</span> <span class="o">=</span> <span class="n">dimattrs</span> <span class="c1"># asserts len(dimattr) == 1</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="n">adims</span> <span class="o">=</span> <span class="n">dimattr</span><span class="p">[</span><span class="n">dimattr</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;(&#39;</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">:</span><span class="n">dimattr</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;)&#39;</span><span class="p">)]</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;,&#39;</span><span class="p">)</span>

        <span class="c1"># See if dimensions are all numeric</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">adims</span> <span class="o">=</span> <span class="p">[</span><span class="nb">int</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">adims</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="c1"># Just check number of dimensions</span>
            <span class="k">if</span> <span class="n">spec_type</span> <span class="o">!=</span> <span class="s1">&#39;character&#39;</span><span class="p">:</span>
                <span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">adims</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">dims</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">return</span> <span class="p">((</span><span class="n">itemsize</span> <span class="o">!=</span> <span class="mi">1</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">dims</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">adims</span><span class="p">))</span> <span class="ow">or</span>   <span class="c1"># arrays of strings, dtype=&#39;S&#39;</span>
                        <span class="p">(</span><span class="n">itemsize</span> <span class="o">==</span> <span class="mi">1</span> <span class="ow">and</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">dims</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">adims</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span> <span class="ow">or</span> <span class="nb">len</span><span class="p">(</span><span class="n">dims</span><span class="p">)</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">adims</span><span class="p">))))</span>   <span class="c1"># arrays of characters, dtype=&#39;S1&#39;</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="c1"># Check each dimension matches</span>
            <span class="k">return</span> <span class="nb">all</span><span class="p">([</span><span class="n">x</span> <span class="o">==</span> <span class="n">y</span> <span class="k">for</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">adims</span><span class="p">,</span><span class="n">dims</span><span class="p">)])</span>

<span class="k">def</span> <span class="nf">process_in_args</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="n">inargs</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">args_str_args</span><span class="p">,</span> <span class="n">routine_name</span><span class="p">):</span>
    <span class="c1"># Process positional arguments</span>
    <span class="n">newargs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">modargs</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="n">arg</span><span class="p">,</span> <span class="n">spec</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">args</span><span class="p">,</span><span class="n">inargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">arg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">):</span>
            <span class="bp">cls</span> <span class="o">=</span> <span class="n">FortranDerivedTypes</span><span class="p">[</span><span class="n">spec</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
                <span class="n">newargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arg</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">)</span>
                <span class="n">modargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">NotImplementedError</span>
                    <span class="n">newarg</span> <span class="o">=</span> <span class="bp">cls</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
                    <span class="n">newargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">newarg</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">)</span>
                    <span class="n">modargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">newarg</span><span class="p">)</span>
                <span class="k">except</span><span class="p">:</span>
                    <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Argument </span><span class="si">%s</span><span class="s1"> should be of type </span><span class="si">%s</span><span class="s1"> but got incompatible type </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">],</span> <span class="nb">type</span><span class="p">(</span><span class="n">arg</span><span class="p">)))</span>

        <span class="k">elif</span> <span class="n">arg</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;character&#39;</span><span class="p">):</span>
            <span class="c1"># if arg is a list of strings of unequal length, pad with spaces</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="nb">list</span><span class="p">)</span> <span class="ow">and</span> <span class="nb">any</span><span class="p">([</span><span class="nb">len</span><span class="p">(</span><span class="n">x</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">len</span><span class="p">(</span><span class="n">arg</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">arg</span><span class="p">]):</span>
                <span class="n">a</span> <span class="o">=</span> <span class="n">s2a</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span><span class="o">.</span><span class="n">T</span>
                <span class="n">newargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
                <span class="n">modargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">newargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
                <span class="n">modargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">newargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">arg</span><span class="p">)</span>
            <span class="n">modargs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="kc">None</span><span class="p">)</span>

    <span class="c1"># Process keyword arguments and args_str arguments.</span>

    <span class="c1"># If we&#39;re expecting an args_str argument, then any kwarg is</span>
    <span class="c1"># permitted. Otherwise kwarg name must match one of the expected</span>
    <span class="c1"># names. If kwarg name matches one of expected values, but type of</span>
    <span class="c1"># argument does not, then we assume it&#39;s an arg_str argument.</span>

    <span class="n">kwarg_lookup</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="n">badnames</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()),</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inargs</span><span class="p">])</span>
    <span class="n">got_args_str</span> <span class="o">=</span> <span class="n">prefix</span><span class="o">+</span><span class="s1">&#39;args_str&#39;</span> <span class="ow">in</span> <span class="n">kwarg_lookup</span>

    <span class="n">newkwargs</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">modkwargs</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">args_str_kwargs</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">a</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
        <span class="n">k</span> <span class="o">=</span> <span class="n">prefix</span><span class="o">+</span><span class="n">k</span>
        <span class="k">if</span> <span class="n">got_args_str</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">k</span> <span class="o">!=</span> <span class="n">prefix</span><span class="o">+</span><span class="s1">&#39;args_str&#39;</span> <span class="ow">and</span> <span class="p">(</span><span class="n">k</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwarg_lookup</span> <span class="ow">or</span> <span class="p">(</span><span class="n">k</span> <span class="ow">in</span> <span class="n">kwarg_lookup</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">type_is_compatible</span><span class="p">(</span><span class="n">kwarg_lookup</span><span class="p">[</span><span class="n">k</span><span class="p">],</span> <span class="n">a</span><span class="p">))):</span>
                <span class="k">if</span> <span class="n">k</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">prefix</span><span class="p">):]</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">args_str_args</span><span class="p">:</span>
                    <span class="n">wraplog</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span><span class="s1">&#39;Converting unexpected keyword argument &quot;</span><span class="si">%s</span><span class="s1">&quot; to routine &quot;</span><span class="si">%s</span><span class="s1">()&quot; to an args_str argument - is this a typo?&#39;</span> <span class="o">%</span>
                                 <span class="p">(</span><span class="n">k</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">prefix</span><span class="p">):],</span> <span class="n">routine_name</span><span class="p">))</span>
                <span class="n">args_str_kwargs</span><span class="p">[</span><span class="n">k</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">prefix</span><span class="p">):]]</span> <span class="o">=</span> <span class="n">a</span>
                <span class="k">continue</span>
        <span class="k">if</span> <span class="n">k</span> <span class="ow">not</span> <span class="ow">in</span> <span class="n">kwarg_lookup</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown keyword argument </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">k</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">kwarg_lookup</span><span class="p">[</span><span class="n">k</span><span class="p">][</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">):</span>
            <span class="bp">cls</span> <span class="o">=</span> <span class="n">FortranDerivedTypes</span><span class="p">[</span><span class="n">kwarg_lookup</span><span class="p">[</span><span class="n">k</span><span class="p">][</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="bp">cls</span><span class="p">):</span>
                <span class="n">newkwargs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span><span class="o">.</span><span class="n">_fpointer</span>
                <span class="n">modkwargs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>
            <span class="k">elif</span> <span class="n">a</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
                <span class="k">continue</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">NotImplementedError</span>
                    <span class="n">na</span> <span class="o">=</span> <span class="bp">cls</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
                    <span class="n">newkwargs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">na</span><span class="o">.</span><span class="n">_fpointer</span>
                    <span class="n">modkwargs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">na</span>
                <span class="k">except</span><span class="p">:</span>
                    <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;Argument </span><span class="si">%s</span><span class="s1"> should be of type </span><span class="si">%s</span><span class="s1">, but got incompatible type </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">a</span><span class="p">,</span><span class="n">kwarg_lookup</span><span class="p">[</span><span class="n">k</span><span class="p">],</span><span class="nb">type</span><span class="p">(</span><span class="n">a</span><span class="p">)))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">a</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">continue</span>
            <span class="n">newkwargs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">a</span>
            <span class="n">modkwargs</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="kc">None</span>

    <span class="c1"># Construct final args_str by merging args_str argument with args_str_kwargs</span>
    <span class="k">if</span> <span class="n">got_args_str</span><span class="p">:</span>
        <span class="n">args_str_final</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">prefix</span><span class="o">+</span><span class="s1">&#39;args_str&#39;</span> <span class="ow">in</span> <span class="n">newkwargs</span><span class="p">:</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">newkwargs</span><span class="p">[</span><span class="n">prefix</span><span class="o">+</span><span class="s1">&#39;args_str&#39;</span><span class="p">],</span> <span class="n">basestring</span><span class="p">):</span>
                <span class="n">args_str_final</span> <span class="o">=</span> <span class="n">args_str_final</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="n">newkwargs</span><span class="p">[</span><span class="n">prefix</span><span class="o">+</span><span class="s1">&#39;args_str&#39;</span><span class="p">]</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">args_str_final</span> <span class="o">=</span> <span class="n">args_str_final</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="n">args_str</span><span class="p">(</span><span class="n">newkwargs</span><span class="p">[</span><span class="n">prefix</span><span class="o">+</span><span class="s1">&#39;args_str&#39;</span><span class="p">])</span>
        <span class="k">if</span> <span class="n">args_str_kwargs</span> <span class="o">!=</span> <span class="p">{}:</span>
            <span class="n">args_str_final</span> <span class="o">=</span> <span class="n">args_str_final</span> <span class="o">+</span> <span class="s2">&quot; &quot;</span> <span class="o">+</span> <span class="n">args_str</span><span class="p">(</span><span class="n">args_str_kwargs</span><span class="p">)</span>
        <span class="k">if</span> <span class="n">args_str_final</span> <span class="o">!=</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
            <span class="n">newkwargs</span><span class="p">[</span><span class="n">prefix</span><span class="o">+</span><span class="s1">&#39;args_str&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">args_str_final</span>

    <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">newargs</span><span class="p">),</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">modargs</span><span class="p">),</span> <span class="n">newkwargs</span><span class="p">,</span> <span class="n">modkwargs</span>

<span class="k">def</span> <span class="nf">process_results</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="n">inargs</span><span class="p">,</span> <span class="n">outargs</span><span class="p">,</span> <span class="n">prefix</span><span class="p">):</span>
    <span class="n">newres</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="n">madeseq</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">if</span> <span class="n">res</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="nb">len</span><span class="p">(</span><span class="n">outargs</span><span class="p">)</span> <span class="o">&lt;=</span> <span class="mi">1</span><span class="p">:</span>
        <span class="n">madeseq</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="n">res</span> <span class="o">=</span> <span class="p">(</span><span class="n">res</span><span class="p">,)</span>

    <span class="c1"># intent(out) arguments form result tuple</span>
    <span class="k">if</span> <span class="n">res</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">r</span><span class="p">,</span> <span class="n">spec</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">res</span><span class="p">,</span><span class="n">outargs</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;type&#39;</span><span class="p">):</span>
                <span class="n">newres</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">FortranDerivedTypes</span><span class="p">[</span><span class="n">spec</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()](</span><span class="n">fpointer</span><span class="o">=</span><span class="n">r</span><span class="p">,</span><span class="n">finalise</span><span class="o">=</span><span class="kc">True</span><span class="p">))</span>
            <span class="k">elif</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">np</span><span class="o">.</span><span class="n">ndarray</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">get_fortran_indexing</span><span class="p">():</span>
                    <span class="c1"># Convert to one-based FortranArray</span>
                    <span class="n">r</span> <span class="o">=</span> <span class="n">r</span><span class="o">.</span><span class="n">view</span><span class="p">(</span><span class="n">FortranArray</span><span class="p">)</span>
                <span class="n">newres</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
            <span class="k">elif</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;logical&#39;</span><span class="p">:</span>
                <span class="n">newres</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">r</span> <span class="o">==</span> <span class="n">QUIPPY_TRUE</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">newres</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>

    <span class="c1"># update any objects in args or kwargs affected by this call</span>
    <span class="k">for</span> <span class="n">arg</span><span class="p">,</span> <span class="n">spec</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">inargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">arg</span><span class="p">,</span> <span class="n">FortranDerivedType</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="s1">&#39;fintent(in)&#39;</span> <span class="ow">in</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]):</span>
            <span class="n">arg</span><span class="o">.</span><span class="n">_update</span><span class="p">()</span>

    <span class="n">type_lookup</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="n">badnames</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()),</span><span class="n">x</span><span class="p">)</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inargs</span><span class="p">])</span>
    <span class="k">for</span> <span class="n">k</span><span class="p">,</span><span class="n">a</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
        <span class="n">k</span> <span class="o">=</span> <span class="n">prefix</span> <span class="o">+</span> <span class="n">k</span>
        <span class="k">if</span> <span class="p">(</span><span class="nb">isinstance</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">FortranDerivedType</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="s1">&#39;fintent(in)&#39;</span> <span class="ow">in</span> <span class="n">type_lookup</span><span class="p">[</span><span class="n">k</span><span class="p">][</span><span class="s1">&#39;attributes&#39;</span><span class="p">]):</span>
            <span class="n">a</span><span class="o">.</span><span class="n">_update</span><span class="p">()</span>

    <span class="k">if</span> <span class="n">res</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="k">return</span> <span class="kc">None</span>
    <span class="k">elif</span> <span class="n">madeseq</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">newres</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">tuple</span><span class="p">(</span><span class="n">newres</span><span class="p">)</span>


<div class="viewcode-block" id="FortranDerivedType"><a class="viewcode-back" href="../../oo_fortran.html#quippy.atoms.FortranDerivedType">[docs]</a><span class="k">class</span> <span class="nc">FortranDerivedType</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Abstract base class for all fortran derived types.</span>

<span class="sd">    This class is an abstract base class for all Fortran</span>
<span class="sd">    derived-types. It contains all the magic necessary</span>
<span class="sd">    to communicate with a module wrapped using the</span>
<span class="sd">    :mod:`f2py_wrapper_gen` module.</span>

<span class="sd">    The constructor invokes a Fortran interface or routine to initialise</span>
<span class="sd">    the object. When the reference count for instances of this class drops to zero,</span>
<span class="sd">    the destructor frees the associated Fortran derived type.</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">_modobj</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">_moddoc</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">_classdoc</span> <span class="o">=</span> <span class="kc">None</span>
    <span class="n">_routines</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">_subobjs</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">_arrays</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">_interfaces</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">_elements</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">_cmp_skip_fields</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">_cmp_tol</span> <span class="o">=</span> <span class="mf">1e-8</span>

    <span class="n">_prefix</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>

        <span class="n">call_init</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_finalise</span> <span class="o">=</span> <span class="kc">True</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update_hooks</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_subobjs_cache</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">if</span> <span class="s1">&#39;fpointer&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fpointer&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">call_init</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fpointer&#39;</span><span class="p">]</span>
            <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;fpointer&#39;</span><span class="p">]</span>

        <span class="k">if</span> <span class="s1">&#39;finalise&#39;</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_finalise</span> <span class="o">=</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;finalise&#39;</span><span class="p">]</span>
            <span class="k">del</span> <span class="n">kwargs</span><span class="p">[</span><span class="s1">&#39;finalise&#39;</span><span class="p">]</span>

        <span class="n">orig_args</span> <span class="o">=</span> <span class="n">args</span>
        <span class="n">orig_kwargs</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">copy</span><span class="p">()</span>

        <span class="k">if</span> <span class="n">call_init</span><span class="p">:</span>
            <span class="k">if</span> <span class="s1">&#39;__init__&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_interfaces</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_runinterface</span><span class="p">(</span><span class="s1">&#39;__init__&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">elif</span> <span class="s1">&#39;__init__&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_routines</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_runroutine</span><span class="p">(</span><span class="s1">&#39;__init__&#39;</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Constructing </span><span class="si">%s</span><span class="s1">(fpointer=</span><span class="si">%r</span><span class="s1">, finalise=</span><span class="si">%r</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_finalise</span><span class="p">))</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">n</span>

<div class="viewcode-block" id="FortranDerivedType.is_same_fortran_object"><a class="viewcode-back" href="../../oo_fortran.html#quippy.atoms.FortranDerivedType.is_same_fortran_object">[docs]</a>    <span class="k">def</span> <span class="nf">is_same_fortran_object</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Test if `self` and `other` point to the same Fortan object.&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="s1">&#39;_fpointer&#39;</span><span class="p">):</span>
            <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="o">==</span> <span class="n">other</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span></div>

<div class="viewcode-block" id="FortranDerivedType.shallow_copy"><a class="viewcode-back" href="../../oo_fortran.html#quippy.atoms.FortranDerivedType.shallow_copy">[docs]</a>    <span class="k">def</span> <span class="nf">shallow_copy</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Return a shallow copy of `self`.&quot;&quot;&quot;</span>
        <span class="n">other</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">()</span>
        <span class="n">other</span><span class="o">.</span><span class="n">shallow_copy_from</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">other</span></div>

<div class="viewcode-block" id="FortranDerivedType.shallow_copy_from"><a class="viewcode-back" href="../../oo_fortran.html#quippy.atoms.FortranDerivedType.shallow_copy_from">[docs]</a>    <span class="k">def</span> <span class="nf">shallow_copy_from</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Transform `self` into a shallow copy of `other`.&quot;&quot;&quot;</span>

        <span class="c1"># free any memory currently associated with `self`</span>
        <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="fm">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>

        <span class="c1"># copy any normal (not Fortran) attributes</span>
        <span class="k">for</span> <span class="n">k</span><span class="p">,</span> <span class="n">v</span> <span class="ow">in</span> <span class="n">other</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">k</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="n">k</span> <span class="ow">not</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">:</span>
                <span class="bp">self</span><span class="o">.</span><span class="vm">__dict__</span><span class="p">[</span><span class="n">k</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="o">=</span> <span class="n">other</span><span class="o">.</span><span class="n">_fpointer</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;ref_count&#39;</span><span class="p">):</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">ref_count</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">ref_count</span> <span class="o">+</span> <span class="mi">1</span></div>

    <span class="k">def</span> <span class="nf">__del__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_finalise</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="c1">#wraplog.debug(&#39;Freeing %s(fpointer=%r, finalise=%r)&#39; % (self.__class__.__name__, self._fpointer, self._finalise))</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;_initialised&#39;</span><span class="p">):</span>
            <span class="k">del</span> <span class="bp">self</span><span class="o">.</span><span class="n">_initialised</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_subobjs_cache</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span> <span class="ow">and</span> <span class="ow">not</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">()</span> <span class="ow">and</span> <span class="s1">&#39;__del__&#39;</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_routines</span><span class="p">:</span>
            <span class="n">fobj</span><span class="p">,</span> <span class="n">doc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_routines</span><span class="p">[</span><span class="s1">&#39;__del__&#39;</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">fobj</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">fobj</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">)</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="o">=</span> <span class="kc">None</span>


    <span class="k">def</span> <span class="nf">__repr__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;&lt;</span><span class="si">%s</span><span class="s1"> object at 0x</span><span class="si">%x</span><span class="s1"> fpointer=None&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="s1">&#39;&lt;</span><span class="si">%s</span><span class="s1"> object at 0x</span><span class="si">%x</span><span class="s1"> fpointer=</span><span class="si">%r</span><span class="s1">&gt;&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">),</span> <span class="nb">tuple</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="o">!=</span> <span class="mi">0</span><span class="p">]))</span>

    <span class="k">def</span> <span class="nf">__str__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">items</span> <span class="o">=</span> <span class="p">[]</span>
        <span class="k">for</span> <span class="n">k</span> <span class="ow">in</span> <span class="nb">dir</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">k</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">):</span> <span class="k">continue</span>
            <span class="n">v</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">k</span><span class="p">)</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">MethodType</span><span class="p">):</span> <span class="k">continue</span>
            <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="n">FortranDerivedType</span><span class="p">):</span>
                <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">=</span><span class="si">%s</span><span class="s1">()&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">v</span><span class="p">,</span> <span class="nb">str</span><span class="p">):</span>
                    <span class="n">v</span> <span class="o">=</span> <span class="n">v</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
                <span class="n">items</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">))</span>
        <span class="k">return</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1">(</span><span class="si">%s</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="s1">&#39;,</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">items</span><span class="p">))</span>

    <span class="k">def</span> <span class="nf">__eq__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="c1"># Class must agree</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="nb">issubclass</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="vm">__class__</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="nb">issubclass</span><span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">):</span>
            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;class mismatch </span><span class="si">%s</span><span class="s1"> not subclass of </span><span class="si">%s</span><span class="s1"> (or vice versa)&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">other</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>
            <span class="k">return</span> <span class="kc">False</span>

        <span class="c1"># Don&#39;t compare uninitialised objects</span>
        <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="s1">&#39;initialised&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">initialised</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">other</span><span class="o">.</span><span class="n">initialised</span><span class="p">:</span>
                <span class="k">return</span> <span class="kc">True</span>
            <span class="k">if</span> <span class="p">((</span><span class="bp">self</span><span class="o">.</span><span class="n">initialised</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">other</span><span class="o">.</span><span class="n">initialised</span><span class="p">)</span> <span class="ow">or</span>
                <span class="p">(</span><span class="n">other</span><span class="o">.</span><span class="n">initialised</span> <span class="ow">and</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">initialised</span><span class="p">)):</span>
                <span class="k">return</span> <span class="kc">False</span>

        <span class="c1"># Compare elements and sub-objects</span>
        <span class="k">for</span> <span class="n">el</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_elements</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subobjs</span><span class="o">.</span><span class="n">keys</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">el</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cmp_skip_fields</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">el</span><span class="p">):</span>
                <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;element in self but not other </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">el</span><span class="p">)</span>
                <span class="k">return</span> <span class="kc">False</span>

            <span class="k">if</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">el</span><span class="p">)</span> <span class="o">!=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">el</span><span class="p">):</span>
                <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;element mismatch </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">el</span><span class="p">)</span>
                <span class="k">return</span> <span class="kc">False</span>

        <span class="c1"># Compare arrays</span>
        <span class="k">for</span> <span class="n">array</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_arrays</span><span class="p">:</span>
            <span class="n">a</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">array</span><span class="p">)</span>
            <span class="n">b</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">other</span><span class="p">,</span> <span class="n">array</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">a</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">and</span> <span class="n">b</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">continue</span>
            <span class="k">if</span> <span class="n">a</span> <span class="ow">is</span> <span class="kc">None</span> <span class="ow">or</span> <span class="n">b</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">return</span> <span class="kc">False</span>
            <span class="k">if</span> <span class="n">a</span><span class="o">.</span><span class="n">dtype</span><span class="o">.</span><span class="n">kind</span> <span class="o">!=</span> <span class="s1">&#39;f&#39;</span><span class="p">:</span>
                <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
                    <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;array mismatch </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">array</span><span class="p">)</span>
                    <span class="k">return</span> <span class="kc">False</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="o">-</span><span class="n">b</span><span class="p">)</span> <span class="o">&gt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">_cmp_tol</span><span class="p">)</span><span class="o">.</span><span class="n">any</span><span class="p">():</span>
                    <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;real array mismatch </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">array</span><span class="p">)</span>
                    <span class="k">return</span> <span class="kc">False</span>
        <span class="k">return</span> <span class="kc">True</span>

    <span class="k">def</span> <span class="nf">__ne__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">other</span><span class="p">):</span>
        <span class="k">return</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="fm">__eq__</span><span class="p">(</span><span class="n">other</span><span class="p">)</span>

<div class="viewcode-block" id="FortranDerivedType._update"><a class="viewcode-back" href="../../oo_fortran.html#quippy.atoms.FortranDerivedType._update">[docs]</a>    <span class="k">def</span> <span class="nf">_update</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Automatically invoked whenever this object needs to be</span>
<span class="sd">        updated. This happens when it is first created, when it is</span>
<span class="sd">        direcly passed to a Fortran routine with ``intent(in,out)`` or</span>
<span class="sd">        when it is a component of another object which is passed with</span>
<span class="sd">        ``intent(in,out)``.</span>

<span class="sd">        This method also invokes all the hook in the</span>
<span class="sd">        :attr:`_update_hooks` list. This can be used in subclasses to</span>
<span class="sd">        allow a customised response. For example this mechanism is used</span>
<span class="sd">        in :class:`quippy.extras.Atoms` to update Atoms properties.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;updating </span><span class="si">%s</span><span class="s1"> at 0x</span><span class="si">%x</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="p">,</span> <span class="nb">id</span><span class="p">(</span><span class="bp">self</span><span class="p">)))</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span> <span class="k">return</span>
        <span class="k">for</span> <span class="n">hook</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_update_hooks</span><span class="p">:</span>
            <span class="n">hook</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span></div>

    <span class="k">def</span> <span class="nf">add_hook</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hook</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update_hooks</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">hook</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">remove_hook</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">hook</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update_hooks</span><span class="o">.</span><span class="n">remove</span><span class="p">(</span><span class="n">hook</span><span class="p">)</span>

<div class="viewcode-block" id="FortranDerivedType._get_array_shape"><a class="viewcode-back" href="../../oo_fortran.html#quippy.atoms.FortranDerivedType._get_array_shape">[docs]</a>    <span class="k">def</span> <span class="nf">_get_array_shape</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        This method can be used to override Fortran&#39;s idea of the shape of</span>
<span class="sd">        arrays within derived types, for example to present only a partial</span>
<span class="sd">        view of an array. This is used in :class:`quippy.table.Table`</span>
<span class="sd">        to allow the sizes of arrays within the Table class to correspond to the</span>
<span class="sd">        current extent of the Table, rather than the size of the allocated storage</span>
<span class="sd">        which will usually be larger.</span>

<span class="sd">        If this method returns ``None`` then the full array is presented, otherwise</span>
<span class="sd">        the return value should be a tuple `(N_1, N_2, ..., N_d)` where `d`</span>
<span class="sd">        is the number of dimensions of the array and `N_1, N_2` etc. are the lengths</span>
<span class="sd">        of each dimension.</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="FortranDerivedType._runroutine"><a class="viewcode-back" href="../../oo_fortran.html#quippy.atoms.FortranDerivedType._runroutine">[docs]</a>    <span class="k">def</span> <span class="nf">_runroutine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Internal method used to invoke the Fortran routine `name`</span>

<span class="sd">        `name` must be a valid key in :attr:`_routines`. Wrapper</span>
<span class="sd">        methods which simply call :meth:`_runroutine()` are</span>
<span class="sd">        automatically generated in subclasses of</span>
<span class="sd">        :class:`FortranDerivedType` by :func:`wrap_all()`.</span>

<span class="sd">        Input arguments which are instances of a subclass of</span>
<span class="sd">        :class:`FortranDerivedType` are replaced by their</span>
<span class="sd">        :attr:`_fpointer` integer attribute.</span>

<span class="sd">        If there is an keyword argument with the name `args_str`</span>
<span class="sd">        then unexpected keyword arguments are permitted. All the</span>
<span class="sd">        undefined keyword arguments are collected together to form a</span>
<span class="sd">        dictionary which is converted to string form and used as the the</span>
<span class="sd">        `arg_string` argument, providing rudimentary support for</span>
<span class="sd">        variable numbers and types of arguments. For example::</span>

<span class="sd">            p = Potential(&#39;IP SW&#39;, xml_string)</span>
<span class="sd">	        p.calc(at, virial=True, energy=True)</span>

<span class="sd">        is equivalent to::</span>

<span class="sd">            p = Potential(&#39;IP SW&#39;, xml_string)</span>
<span class="sd">	        p.calc(at, args_str=&quot;virial energy&quot;)</span>

<span class="sd">        The return value us made up of a tuple of the arguments to the</span>
<span class="sd">        Fortran routine which are ``intent(out)``. Pointers to Fortran</span>
<span class="sd">        derived-type instances are replaced with new instances of the</span>
<span class="sd">        appropriate subclass of :class:`FortranDerivedType`. Arrays</span>
<span class="sd">        are converted to use one-based indexing using</span>
<span class="sd">        :class:`~quippy.farray.FortranArray`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;__init__&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> object not initialised.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_routines</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">NameError</span><span class="p">(</span><span class="s1">&#39;Unknown fortran routine: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

        <span class="n">fobj</span><span class="p">,</span> <span class="n">doc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_routines</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>

        <span class="c1">#inargs  = filter(lambda x: not &#39;intent(out)&#39; in x[&#39;attributes&#39;], doc[&#39;args&#39;])</span>
        <span class="c1">#outargs = filter(lambda x: &#39;intent(out)&#39; in x[&#39;attributes&#39;], doc[&#39;args&#39;])</span>

        <span class="n">inargs</span>  <span class="o">=</span> <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">doc</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="ow">not</span> <span class="s1">&#39;intent(out)&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>
        <span class="n">outargs</span> <span class="o">=</span> <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">doc</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="s1">&#39;intent(out)&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>
        <span class="n">args_str_args</span> <span class="o">=</span> <span class="n">doc</span><span class="p">[</span><span class="s1">&#39;args_str&#39;</span><span class="p">]</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;__init__&#39;</span><span class="p">):</span>
            <span class="c1"># Put self at beginning of args list</span>
            <span class="n">args</span> <span class="o">=</span> <span class="nb">tuple</span><span class="p">([</span><span class="bp">self</span><span class="p">]</span> <span class="o">+</span> <span class="nb">list</span><span class="p">(</span><span class="n">args</span><span class="p">))</span>
        <span class="n">newargs</span><span class="p">,</span> <span class="n">modargs</span><span class="p">,</span> <span class="n">newkwargs</span><span class="p">,</span> <span class="n">modkwargs</span> <span class="o">=</span> <span class="n">process_in_args</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="n">inargs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefix</span><span class="p">,</span> <span class="n">args_str_args</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">res</span> <span class="o">=</span> <span class="n">fobj</span><span class="p">(</span><span class="o">*</span><span class="n">newargs</span><span class="p">,</span> <span class="o">**</span><span class="n">newkwargs</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">RuntimeError</span><span class="p">:</span>
            <span class="k">raise</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">exctype</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">tb</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()</span>

                <span class="k">if</span> <span class="n">is_interactive_shell</span><span class="p">():</span>
                    <span class="n">error_str</span> <span class="o">=</span> <span class="s1">&#39;Fortran routine </span><span class="si">%s</span><span class="s1">(</span><span class="si">%s</span><span class="s1">):</span><span class="se">\n</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">a</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">args</span><span class="p">)</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">=</span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">iteritems</span><span class="p">()]),</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># Remove Fortran traceback</span>
                    <span class="n">error_str</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>

                <span class="k">raise</span> <span class="n">exctype</span><span class="p">,</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="n">error_str</span><span class="p">),</span> <span class="n">tb</span>

            <span class="k">finally</span><span class="p">:</span>
                <span class="k">del</span> <span class="n">tb</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="k">raise</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;__init__&#39;</span><span class="p">):</span>
            <span class="n">newres</span> <span class="o">=</span> <span class="n">process_results</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="n">inargs</span><span class="p">,</span> <span class="n">outargs</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefix</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">newres</span> <span class="o">=</span> <span class="n">res</span>

        <span class="k">return</span> <span class="n">newres</span></div>


<div class="viewcode-block" id="FortranDerivedType._runinterface"><a class="viewcode-back" href="../../oo_fortran.html#quippy.atoms.FortranDerivedType._runinterface">[docs]</a>    <span class="k">def</span> <span class="nf">_runinterface</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Internal method used to invoke the appropriate routine</span>
<span class="sd">        within the Fortran interface `name`. If no routine is found</span>
<span class="sd">        matching the names and types of the arguments provided then</span>
<span class="sd">        an :exc:`TypeError` exception is raised.</span>

<span class="sd">        Arguments and results are handled in the same way as</span>
<span class="sd">        :func:`_runroutine`.</span>
<span class="sd">        &quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_interfaces</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown interface </span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>

        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Interface </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">rname</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">routine</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_interfaces</span><span class="p">[</span><span class="n">name</span><span class="p">]:</span>

            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Trying candidate routine </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">rname</span><span class="p">)</span>

            <span class="c1">#inargs = filter(lambda x: not &#39;intent(out)&#39; in x[&#39;attributes&#39;], spec[&#39;args&#39;])</span>
            <span class="n">inargs</span>  <span class="o">=</span> <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="ow">not</span> <span class="s1">&#39;intent(out)&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;__init__&#39;</span><span class="p">):</span>
                <span class="n">inargs</span> <span class="o">=</span> <span class="n">inargs</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="c1"># remove self argument</span>

            <span class="n">oblig_inargs</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inargs</span> <span class="k">if</span>  <span class="ow">not</span> <span class="s1">&#39;optional&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>
            <span class="n">opt_inargs</span>   <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inargs</span> <span class="k">if</span> <span class="s1">&#39;optional&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>

            <span class="c1"># Check number of arguments is compatible</span>
            <span class="c1"># Should be in range len(oblig_inargs) &lt;= L &lt;= len(oblig_inargs) + len(opt_inargs)</span>
            <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">oblig_inargs</span><span class="p">)</span> <span class="ow">or</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">oblig_inargs</span><span class="p">)</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">opt_inargs</span><span class="p">)):</span>
                <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Number of arguments incompatible: </span><span class="si">%d</span><span class="s1"> must be in range </span><span class="si">%d</span><span class="s1"> &lt;= n &lt;= </span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span>
                              <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">oblig_inargs</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">oblig_inargs</span><span class="p">)</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="n">opt_inargs</span><span class="p">)))</span>
                <span class="k">continue</span>

            <span class="n">newinargs</span> <span class="o">=</span> <span class="p">(</span><span class="n">oblig_inargs</span> <span class="o">+</span> <span class="n">opt_inargs</span><span class="p">)[:</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)]</span>

            <span class="c1"># Check types and dimensions are compatible</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">([</span><span class="n">type_is_compatible</span><span class="p">(</span><span class="n">spec</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">spec</span><span class="p">,</span><span class="n">a</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">newinargs</span><span class="p">,</span> <span class="n">args</span><span class="p">)]):</span>
                <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Types and dimensions of args incompatible </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                              <span class="p">(</span><span class="n">newinargs</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="p">[</span><span class="n">type_is_compatible</span><span class="p">(</span><span class="n">spec</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">spec</span><span class="p">,</span><span class="n">a</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">newinargs</span><span class="p">,</span> <span class="n">args</span><span class="p">)]))</span>
                <span class="k">continue</span>

            <span class="c1"># Check keyword arguments, if incompatible continue to next</span>
            <span class="c1"># candidate interface</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="n">innames</span> <span class="o">=</span> <span class="p">[</span><span class="n">badnames</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">],</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">oblig_inargs</span> <span class="o">+</span> <span class="n">opt_inargs</span><span class="p">]</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">([</span><span class="bp">self</span><span class="o">.</span><span class="n">_prefix</span><span class="o">+</span><span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="n">innames</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">):]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">keys</span><span class="p">()]):</span>
                <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Unexpected keyword argument valid=</span><span class="si">%s</span><span class="s1"> got=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">innames</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">):]))</span>
                <span class="k">continue</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
                    <span class="p">(</span><span class="n">inarg</span><span class="p">,)</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inargs</span> <span class="k">if</span> <span class="n">badnames</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">],</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="bp">self</span><span class="o">.</span><span class="n">_prefix</span><span class="o">+</span><span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">type_is_compatible</span><span class="p">(</span><span class="n">inarg</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
                        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Types and dimensions of kwarg </span><span class="si">%s</span><span class="s1"> incompatible&#39;</span> <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span>

            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;calling &#39;</span><span class="o">+</span><span class="n">rname</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">routine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>


        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;No matching routine found in interface </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span></div>



    <span class="k">def</span> <span class="nf">_runinterface_simple</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_interfaces</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown interface </span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>

        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Interface </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>

        <span class="k">for</span> <span class="n">rname</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">routine</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_interfaces</span><span class="p">[</span><span class="n">name</span><span class="p">]:</span>
            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Trying candidate routine </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">rname</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">routine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="ne">RuntimeError</span><span class="p">):</span>
                    <span class="k">raise</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">continue</span>

        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;No matching routine found in interface </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span></div>


<span class="k">def</span> <span class="nf">nested_dict_update</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">w</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">mod</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">:</span>
        <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">w</span><span class="p">)</span> <span class="o">==</span> <span class="nb">list</span><span class="p">:</span>
            <span class="n">mod</span> <span class="o">+=</span> <span class="n">w</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">&#39;First argument is list, but second is not.&#39;</span><span class="p">)</span>
    <span class="k">elif</span> <span class="nb">type</span><span class="p">(</span><span class="n">mod</span><span class="p">)</span> <span class="o">==</span> <span class="nb">str</span><span class="p">:</span>
        <span class="n">mod</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">w</span>
    <span class="k">else</span><span class="p">:</span>

        <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">w</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">mod</span><span class="p">:</span>
                <span class="n">nested_dict_update</span><span class="p">(</span><span class="n">mod</span><span class="p">[</span><span class="n">key</span><span class="p">],</span> <span class="n">value</span><span class="p">)</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">mod</span><span class="p">[</span><span class="n">key</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>


<span class="k">def</span> <span class="nf">flatten_list_of_dicts</span><span class="p">(</span><span class="n">dictlist</span><span class="p">):</span>
    <span class="k">if</span> <span class="nb">type</span><span class="p">(</span><span class="n">dictlist</span><span class="p">)</span> <span class="o">==</span> <span class="nb">dict</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">dictlist</span>

    <span class="n">modout</span> <span class="o">=</span> <span class="p">{</span> <span class="p">}</span>
    <span class="k">for</span> <span class="n">d</span> <span class="ow">in</span> <span class="n">dictlist</span><span class="p">:</span>
        <span class="n">nested_dict_update</span><span class="p">(</span><span class="n">modout</span><span class="p">,</span> <span class="n">d</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">modout</span>


<div class="viewcode-block" id="wrap_all"><a class="viewcode-back" href="../../oo_fortran.html#quippy.atoms.wrap_all">[docs]</a><span class="k">def</span> <span class="nf">wrap_all</span><span class="p">(</span><span class="n">fobj</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">mods</span><span class="p">,</span> <span class="n">merge_mods</span><span class="p">,</span> <span class="n">short_names</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">package</span><span class="p">,</span> <span class="n">modules_name_map</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Returns tuple `(classes, routines, params)` suitable for</span>
<span class="sd">    importing into top-level package namespace. `topmod` should be an</span>
<span class="sd">    f2py-generated module containing `fortran` objects, and `spec`</span>
<span class="sd">    should be the specification dictionary generated by</span>
<span class="sd">    :func:`f2py_wrapper_gen.wrap_mod`.  `mods` is a list of the names</span>
<span class="sd">    of Fortran modules to wrap, and `short_names` is a dictionary</span>
<span class="sd">    mapping shortened Fortran derived-type names to their canonical</span>
<span class="sd">    form.</span>

<span class="sd">    `classes` and `routines` are lists of `(name, value)` tuples</span>
<span class="sd">    where `value` is a newly defined subclass of</span>
<span class="sd">    :class:`FortranDerivedType` or newly wrapped routine respectively.</span>
<span class="sd">    `params` is a Python dictionary of Fortran parameters (constants).</span>

<span class="sd">    Here&#39;s how this function is used in quippy&#39;s :file:`__init.py__` to</span>
<span class="sd">    import the new classes, routines and params into the top-level quippy</span>
<span class="sd">    namespace::</span>

<span class="sd">       classes, routines, params = wrap_all(_quippy, spec, spec[&#39;wrap_modules&#39;], spec[&#39;short_names&#39;])</span>

<span class="sd">       for name, cls in classes:</span>
<span class="sd"> 	  setattr(sys.modules[__name__], name, cls)</span>

<span class="sd">       for name, routine in routines:</span>
<span class="sd">   	  setattr(sys.modules[__name__], name, routine)</span>

<span class="sd">       sys.modules[__name__].__dict__.update(params)</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="n">all_classes</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">leftover_routines</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">interfaces</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">top_level_routines</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">all_params</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="n">pymods</span> <span class="o">=</span> <span class="p">{}</span>

    <span class="k">for</span> <span class="n">mod</span><span class="p">,</span> <span class="n">modfile</span> <span class="ow">in</span> <span class="n">mods</span><span class="p">:</span>
        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Module &#39;</span><span class="o">+</span><span class="nb">str</span><span class="p">(</span><span class="n">mod</span><span class="p">))</span>
        <span class="k">if</span> <span class="n">mod</span> <span class="ow">in</span> <span class="n">merge_mods</span><span class="p">:</span>
            <span class="n">curspec</span> <span class="o">=</span> <span class="n">flatten_list_of_dicts</span><span class="p">([</span><span class="n">spec</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">merge_mods</span><span class="p">[</span><span class="n">mod</span><span class="p">]])</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">curspec</span> <span class="o">=</span> <span class="n">spec</span><span class="p">[</span><span class="n">mod</span><span class="p">]</span>

        <span class="n">pymodname</span> <span class="o">=</span> <span class="n">package</span><span class="o">+</span><span class="s1">&#39;.&#39;</span><span class="o">+</span><span class="n">modules_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span><span class="n">mod</span><span class="p">)</span>

        <span class="n">classes</span><span class="p">,</span> <span class="n">routines</span><span class="p">,</span> <span class="n">mod_params</span> <span class="o">=</span> <span class="n">wrapmod</span><span class="p">(</span><span class="n">fobj</span><span class="p">,</span> <span class="n">curspec</span><span class="p">,</span> <span class="n">modname</span><span class="o">=</span><span class="n">mod</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">,</span>
                                                <span class="n">short_names</span><span class="o">=</span><span class="n">short_names</span><span class="p">,</span>
                                                <span class="n">params</span><span class="o">=</span><span class="n">all_params</span><span class="p">,</span> <span class="n">prefix</span><span class="o">=</span><span class="n">prefix</span><span class="p">,</span>
                                                <span class="n">pymodname</span><span class="o">=</span><span class="n">pymodname</span><span class="p">)</span>
        <span class="n">all_classes</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span><span class="n">classes</span><span class="p">)</span>

        <span class="n">pymod</span> <span class="o">=</span> <span class="n">imp</span><span class="o">.</span><span class="n">new_module</span><span class="p">(</span><span class="n">pymodname</span><span class="p">)</span>
        <span class="n">pymod</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">process_docstring</span><span class="p">(</span><span class="n">curspec</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])</span>
        <span class="n">pymod</span><span class="o">.</span><span class="n">__package__</span> <span class="o">=</span> <span class="n">package</span>

        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="bp">cls</span> <span class="ow">in</span> <span class="n">classes</span><span class="p">:</span>
            <span class="bp">cls</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">=</span> <span class="n">pymod</span><span class="o">.</span><span class="vm">__name__</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">pymod</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="bp">cls</span><span class="p">)</span>

        <span class="n">pymod</span><span class="o">.</span><span class="vm">__dict__</span><span class="o">.</span><span class="n">update</span><span class="p">(</span><span class="n">mod_params</span><span class="p">)</span>
        <span class="n">pymods</span><span class="p">[</span><span class="n">modules_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span><span class="n">mod</span><span class="p">)]</span> <span class="o">=</span> <span class="n">pymod</span>

        <span class="c1"># make classes and params symbols public</span>
        <span class="c1"># (available with &#39;from ... import *&#39;)</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">pymod</span><span class="p">,</span> <span class="s1">&#39;__all__&#39;</span><span class="p">,</span> <span class="p">[</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">classes</span><span class="p">]</span><span class="o">+</span><span class="n">mod_params</span><span class="o">.</span><span class="n">keys</span><span class="p">())</span>

        <span class="n">leftover_routines</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">mod</span><span class="p">,</span> <span class="n">curspec</span><span class="p">,</span> <span class="n">routines</span><span class="p">,</span> <span class="n">modfile</span><span class="p">))</span>

    <span class="c1"># add orphaned routines to classes and generate top-level interfaces</span>
    <span class="k">for</span> <span class="n">mod</span><span class="p">,</span> <span class="n">modspec</span><span class="p">,</span> <span class="n">routines</span><span class="p">,</span> <span class="n">modfile</span> <span class="ow">in</span> <span class="n">leftover_routines</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">routine</span> <span class="ow">in</span> <span class="n">routines</span><span class="p">:</span>
            <span class="n">rspec</span> <span class="o">=</span> <span class="n">modspec</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">routine</span><span class="p">]</span>

            <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">rspec</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">])</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span>
                <span class="n">rspec</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="n">FortranDerivedTypes</span> <span class="ow">and</span>
                <span class="n">rspec</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="n">prefix</span><span class="o">+</span><span class="s1">&#39;this&#39;</span><span class="p">):</span>

                <span class="bp">cls</span> <span class="o">=</span> <span class="n">FortranDerivedTypes</span><span class="p">[</span><span class="n">rspec</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span>

                <span class="n">lclsname</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="o">.</span><span class="n">lower</span><span class="p">()[</span><span class="nb">len</span><span class="p">(</span><span class="n">fortran_class_prefix</span><span class="p">):]</span>

                <span class="k">if</span> <span class="n">routine</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">lclsname</span><span class="o">+</span><span class="s1">&#39;_&#39;</span><span class="p">):</span>
                    <span class="n">method_name</span> <span class="o">=</span> <span class="n">routine</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">lclsname</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
                <span class="k">elif</span> <span class="n">lclsname</span> <span class="ow">in</span> <span class="n">short_names</span> <span class="ow">and</span> <span class="n">routine</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="n">short_names</span><span class="p">[</span><span class="n">lclsname</span><span class="p">]</span><span class="o">+</span><span class="s1">&#39;_&#39;</span><span class="p">):</span>
                    <span class="n">method_name</span> <span class="o">=</span> <span class="n">routine</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">short_names</span><span class="p">[</span><span class="n">lclsname</span><span class="p">])</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">method_name</span> <span class="o">=</span> <span class="n">routine</span>

                <span class="n">method_name</span> <span class="o">=</span> <span class="n">py_keywords_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">method_name</span><span class="p">,</span> <span class="n">method_name</span><span class="p">)</span>
                <span class="n">method_name</span> <span class="o">=</span> <span class="n">special_names</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">method_name</span><span class="p">,</span> <span class="n">method_name</span><span class="p">)</span>

                <span class="n">wrapped_routine</span> <span class="o">=</span> <span class="n">wraproutine</span><span class="p">(</span><span class="n">fobj</span><span class="p">,</span> <span class="n">modspec</span><span class="p">,</span> <span class="n">routine</span><span class="p">,</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="o">+</span><span class="s1">&#39;.&#39;</span><span class="o">+</span><span class="n">method_name</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span>
                                              <span class="n">skip_this</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>
                <span class="n">FortranRoutines</span><span class="p">[</span><span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="o">+</span><span class="s1">&#39;.&#39;</span><span class="o">+</span><span class="n">method_name</span><span class="p">]</span> <span class="o">=</span> <span class="n">wrapped_routine</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">method_name</span><span class="p">,</span> <span class="n">wrapped_routine</span><span class="p">)</span>
                <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  added method </span><span class="si">%s</span><span class="s1"> to class </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">method_name</span><span class="p">,</span> <span class="bp">cls</span><span class="o">.</span><span class="vm">__name__</span><span class="p">))</span>

            <span class="k">else</span><span class="p">:</span>
                <span class="n">wrapped_routine</span> <span class="o">=</span> <span class="n">wraproutine</span><span class="p">(</span><span class="n">fobj</span><span class="p">,</span> <span class="n">modspec</span><span class="p">,</span> <span class="n">routine</span><span class="p">,</span> <span class="n">routine</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span>
                                              <span class="n">skip_this</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>
                <span class="k">for</span> <span class="n">intf_name</span><span class="p">,</span><span class="n">intf_spec</span> <span class="ow">in</span> <span class="n">modspec</span><span class="p">[</span><span class="s1">&#39;interfaces&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
                    <span class="k">if</span> <span class="n">routine</span> <span class="ow">in</span> <span class="n">intf_spec</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">]:</span>
                        <span class="k">if</span> <span class="ow">not</span> <span class="n">intf_name</span> <span class="ow">in</span> <span class="n">interfaces</span><span class="p">:</span> <span class="n">interfaces</span><span class="p">[</span><span class="n">intf_name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">intf_spec</span><span class="p">,</span> <span class="p">[],</span> <span class="n">modfile</span><span class="p">)</span>
                        <span class="n">interfaces</span><span class="p">[</span><span class="n">intf_name</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">routine</span><span class="p">,</span> <span class="n">rspec</span><span class="p">,</span> <span class="n">wrapped_routine</span><span class="p">,</span> <span class="n">modfile</span><span class="p">))</span>
                        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  added routine </span><span class="si">%s</span><span class="s1"> to top-level interface </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">routine</span><span class="p">,</span> <span class="n">intf_name</span><span class="p">))</span>
                        <span class="k">break</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">FortranRoutines</span><span class="p">[</span><span class="n">routine</span><span class="p">]</span> <span class="o">=</span> <span class="n">wrapped_routine</span>
                    <span class="n">top_level_routines</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">routine</span><span class="p">,</span><span class="n">wrapped_routine</span><span class="p">))</span>
                    <span class="n">wrapped_routine</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">=</span> <span class="n">pymods</span><span class="p">[</span><span class="n">modules_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span><span class="n">mod</span><span class="p">)]</span><span class="o">.</span><span class="vm">__name__</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="n">pymods</span><span class="p">[</span><span class="n">modules_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span><span class="n">mod</span><span class="p">)],</span> <span class="n">routine</span><span class="p">,</span> <span class="n">wrapped_routine</span><span class="p">)</span>
                    <span class="n">pymods</span><span class="p">[</span><span class="n">modules_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span><span class="n">mod</span><span class="p">)]</span><span class="o">.</span><span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">routine</span><span class="p">)</span>
                    <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  added top-level routine </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">routine</span><span class="p">)</span>

    <span class="c1"># remap interface names which clash with keywords and skip overloaded operators</span>
    <span class="n">interfaces</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="n">py_keywords_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">k</span><span class="p">),</span><span class="n">v</span><span class="p">)</span>
                         <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">interfaces</span><span class="o">.</span><span class="n">iteritems</span><span class="p">()</span> <span class="p">])</span>
    <span class="n">interfaces</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">([(</span><span class="n">special_names</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">k</span><span class="p">),</span><span class="n">v</span><span class="p">)</span>
                         <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">interfaces</span><span class="o">.</span><span class="n">iteritems</span><span class="p">()</span> <span class="p">])</span>

    <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="p">(</span><span class="n">mod</span><span class="p">,</span> <span class="n">intf_spec</span><span class="p">,</span> <span class="n">routines</span><span class="p">,</span> <span class="n">modfile</span><span class="p">)</span> <span class="ow">in</span> <span class="n">interfaces</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
        <span class="n">wrapped_interface</span> <span class="o">=</span> <span class="n">wrapinterface</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">intf_spec</span><span class="p">,</span> <span class="n">routines</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>
        <span class="n">FortranRoutines</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">wrapped_interface</span>
        <span class="n">top_level_routines</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">name</span><span class="p">,</span> <span class="n">wrapped_interface</span><span class="p">))</span>
        <span class="n">wrapped_interface</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">=</span> <span class="n">pymods</span><span class="p">[</span><span class="n">modules_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span><span class="n">mod</span><span class="p">)]</span><span class="o">.</span><span class="vm">__name__</span>
        <span class="nb">setattr</span><span class="p">(</span><span class="n">pymods</span><span class="p">[</span><span class="n">modules_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span><span class="n">mod</span><span class="p">)],</span> <span class="n">name</span><span class="p">,</span> <span class="n">wrapped_interface</span><span class="p">)</span>
        <span class="n">pymods</span><span class="p">[</span><span class="n">modules_name_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">mod</span><span class="p">,</span><span class="n">mod</span><span class="p">)]</span><span class="o">.</span><span class="n">__all__</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  added interface routine </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">routine</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">pymods</span></div>



<span class="k">def</span> <span class="nf">wrapmod</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">moddoc</span><span class="p">,</span> <span class="n">modname</span><span class="p">,</span> <span class="n">modfile</span><span class="p">,</span> <span class="n">short_names</span><span class="p">,</span> <span class="n">params</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">pymodname</span><span class="p">):</span>

    <span class="n">wrapmethod</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">name</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_runroutine</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="n">wrapinterface</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">name</span><span class="p">:</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">:</span> <span class="bp">self</span><span class="o">.</span><span class="n">_runinterface</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="k">def</span> <span class="nf">wrapinit</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">doc</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="n">init</span> <span class="o">=</span> <span class="k">lambda</span> <span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">:</span> <span class="n">FortranDerivedType</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
        <span class="n">init</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">doc</span>
        <span class="k">return</span> <span class="n">init</span>

    <span class="n">routines</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()]</span>
    <span class="n">types</span> <span class="o">=</span>  <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;types&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()]</span>

    <span class="n">classes</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="k">for</span> <span class="bp">cls</span> <span class="ow">in</span> <span class="n">types</span><span class="p">:</span>
        <span class="n">lcls</span> <span class="o">=</span> <span class="bp">cls</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>

        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  Class </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="bp">cls</span><span class="p">)</span>

        <span class="n">method_names</span> <span class="o">=</span> <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
                    <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">x</span><span class="p">][</span><span class="s1">&#39;args&#39;</span><span class="p">])</span> <span class="o">&gt;</span> <span class="mi">0</span> <span class="ow">and</span>
                        <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">x</span><span class="p">][</span><span class="s1">&#39;args&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;type(</span><span class="si">%s</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="n">lcls</span><span class="p">)</span> <span class="p">]</span>

        <span class="c1"># Preferentially use initialise_ptr, finalise_ptr if available since this</span>
        <span class="c1"># does proper reference counting.</span>
        <span class="n">constructors</span> <span class="o">=</span> \
            <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">method_names</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_initialise_ptr&#39;</span> <span class="o">%</span> <span class="n">lcls</span><span class="p">)</span> <span class="p">]</span> <span class="o">+</span>\
            <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">method_names</span> <span class="k">if</span> <span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_initialise&#39;</span> <span class="o">%</span> <span class="n">lcls</span><span class="p">)</span> <span class="ow">or</span>
                                     <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_init&#39;</span> <span class="o">%</span> <span class="n">lcls</span><span class="p">)</span> <span class="ow">or</span>
                                     <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_allocate&#39;</span> <span class="o">%</span> <span class="n">lcls</span><span class="p">))]</span>

        <span class="n">destructors</span> <span class="o">=</span> \
            <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">method_names</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_finalise_ptr&#39;</span> <span class="o">%</span> <span class="n">lcls</span><span class="p">)]</span><span class="o">+</span>\
            <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">method_names</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_finalise&#39;</span> <span class="o">%</span> <span class="n">lcls</span><span class="p">)]</span>


        <span class="k">if</span> <span class="n">lcls</span> <span class="ow">in</span> <span class="n">short_names</span><span class="p">:</span>
            <span class="n">scls</span> <span class="o">=</span> <span class="n">short_names</span><span class="p">[</span><span class="n">lcls</span><span class="p">]</span>
            <span class="n">constructors</span> <span class="o">+=</span> \
                <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">method_names</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_initialise_ptr&#39;</span> <span class="o">%</span> <span class="n">scls</span><span class="p">)</span>
                  <span class="ow">and</span> <span class="s1">&#39;intent(out)&#39;</span> <span class="ow">in</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">x</span><span class="p">][</span><span class="s1">&#39;args&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span> <span class="o">+</span> \
                <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">method_names</span> <span class="k">if</span><span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_initialise&#39;</span> <span class="o">%</span> <span class="n">scls</span><span class="p">)</span> <span class="ow">or</span>
                                        <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_allocate&#39;</span> <span class="o">%</span> <span class="n">scls</span><span class="p">))</span>
                  <span class="ow">and</span> <span class="s1">&#39;intent(out)&#39;</span> <span class="ow">in</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">x</span><span class="p">][</span><span class="s1">&#39;args&#39;</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>

            <span class="n">destructors</span> <span class="o">+=</span> \
                <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">method_names</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_finalise_ptr&#39;</span> <span class="o">%</span> <span class="n">scls</span><span class="p">)</span> <span class="p">]</span> <span class="o">+</span> \
                <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">method_names</span> <span class="k">if</span> <span class="n">x</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">_finalise&#39;</span> <span class="o">%</span> <span class="n">scls</span><span class="p">)</span> <span class="p">]</span>

        <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">constructors</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">):</span>
            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Can&#39;t find constructor for type </span><span class="si">%s</span><span class="s2">. Skipping class&quot;</span> <span class="o">%</span> <span class="bp">cls</span><span class="p">)</span>
            <span class="k">continue</span>

        <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">destructors</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">):</span>
            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s2">&quot;Can&#39;t find destructor for type </span><span class="si">%s</span><span class="s2">. Skipping class&quot;</span> <span class="o">%</span> <span class="bp">cls</span><span class="p">)</span>
            <span class="k">continue</span>

        <span class="n">destructor</span> <span class="o">=</span> <span class="n">destructors</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
        <span class="n">constructor</span> <span class="o">=</span> <span class="n">constructors</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

        <span class="c1"># Name of class, in TitleCase</span>
        <span class="n">tcls</span> <span class="o">=</span> <span class="n">fortran_class_prefix</span><span class="o">+</span><span class="bp">cls</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">upper</span><span class="p">()</span><span class="o">+</span><span class="bp">cls</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span>
        <span class="n">tcls</span> <span class="o">=</span> <span class="n">tcls</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">fortran_class_prefix</span><span class="p">):]</span>

        <span class="k">if</span> <span class="n">constructor</span><span class="p">:</span>
            <span class="n">constructor_name</span> <span class="o">=</span> <span class="n">constructor</span>
            <span class="n">constructor</span> <span class="o">=</span> <span class="n">add_doc</span><span class="p">(</span><span class="n">wrapinit</span><span class="p">(</span><span class="n">constructor</span><span class="p">),</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">prefix</span><span class="o">+</span><span class="n">constructor</span><span class="p">),</span>
                                  <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">constructor</span><span class="p">],</span> <span class="n">constructor</span><span class="p">,</span> <span class="n">tcls</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span>
                                  <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;numpydoc&#39;</span><span class="p">,</span> <span class="n">skip_this</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>

        <span class="n">_routines</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">_interfaces</span> <span class="o">=</span> <span class="p">{}</span>
        <span class="n">methods</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">if</span> <span class="n">constructor</span><span class="p">:</span>
            <span class="n">_routines</span><span class="p">[</span><span class="s1">&#39;__init__&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">prefix</span><span class="o">+</span><span class="n">constructor_name</span><span class="p">),</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">constructor_name</span><span class="p">])</span>

        <span class="k">if</span> <span class="n">destructor</span><span class="p">:</span>
            <span class="n">_routines</span><span class="p">[</span><span class="s1">&#39;__del__&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">prefix</span><span class="o">+</span><span class="n">destructor</span><span class="p">),</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">destructor</span><span class="p">])</span>

        <span class="n">interfaces</span> <span class="o">=</span> <span class="p">{}</span>

        <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">method_names</span><span class="p">:</span>
            <span class="n">fullname</span> <span class="o">=</span> <span class="n">name</span>
            <span class="k">if</span> <span class="n">name</span><span class="p">[:</span><span class="nb">len</span><span class="p">(</span><span class="n">lcls</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">lcls</span><span class="o">+</span><span class="s1">&#39;_&#39;</span><span class="p">:</span>
                <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">lcls</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>

            <span class="k">if</span> <span class="n">lcls</span> <span class="ow">in</span> <span class="n">short_names</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">name</span><span class="p">[:</span><span class="nb">len</span><span class="p">(</span><span class="n">short_names</span><span class="p">[</span><span class="n">lcls</span><span class="p">])</span><span class="o">+</span><span class="mi">1</span><span class="p">]</span> <span class="o">==</span> <span class="n">short_names</span><span class="p">[</span><span class="n">lcls</span><span class="p">]</span><span class="o">+</span><span class="s1">&#39;_&#39;</span><span class="p">:</span>
                    <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">short_names</span><span class="p">[</span><span class="n">lcls</span><span class="p">])</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span>

            <span class="n">name</span> <span class="o">=</span> <span class="n">py_keywords_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">special_names</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

            <span class="n">fobj</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">prefix</span><span class="o">+</span><span class="n">fullname</span><span class="p">)</span>
            <span class="n">doc</span> <span class="o">=</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">fullname</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">fullname</span> <span class="ow">in</span> <span class="n">constructors</span><span class="p">:</span>
                <span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;__init__&#39;</span><span class="o">+</span><span class="n">name</span>
                <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    adding constructor </span><span class="si">%s</span><span class="s1">.</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">tcls</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>

            <span class="k">if</span> <span class="n">fullname</span> <span class="ow">in</span> <span class="n">destructors</span><span class="p">:</span>
                <span class="k">del</span> <span class="n">routines</span><span class="p">[</span><span class="n">routines</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">fullname</span><span class="p">)]</span>
                <span class="k">continue</span>

            <span class="n">_routines</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">fobj</span><span class="p">,</span> <span class="n">doc</span><span class="p">)</span>
            <span class="n">func</span> <span class="o">=</span> <span class="n">add_doc</span><span class="p">(</span><span class="n">wrapmethod</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">fobj</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">fullname</span><span class="p">,</span> <span class="n">tcls</span><span class="o">+</span><span class="s1">&#39;.&#39;</span><span class="o">+</span><span class="n">name</span><span class="p">,</span>
                           <span class="n">prefix</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;numpydoc&#39;</span><span class="p">,</span> <span class="n">skip_this</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>
            <span class="n">func</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">=</span> <span class="n">pymodname</span>
            <span class="n">methods</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">func</span>

            <span class="k">for</span> <span class="n">intf_name</span><span class="p">,</span><span class="n">value</span> <span class="ow">in</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;interfaces&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
                <span class="n">intf_routines</span> <span class="o">=</span> <span class="n">value</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">fullname</span> <span class="ow">in</span> <span class="n">intf_routines</span><span class="p">:</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">intf_name</span> <span class="ow">in</span> <span class="n">interfaces</span><span class="p">:</span>
                        <span class="n">interfaces</span><span class="p">[</span><span class="n">intf_name</span><span class="p">]</span> <span class="o">=</span> <span class="p">[]</span>

                    <span class="c1"># regenerate interface documentation in sphinx format and with correct names</span>
                    <span class="n">intf_func</span> <span class="o">=</span> <span class="n">add_doc</span><span class="p">(</span><span class="n">wrapmethod</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">fobj</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">fullname</span><span class="p">,</span> <span class="n">intf_name</span><span class="p">,</span>
                                        <span class="n">prefix</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;sphinx&#39;</span><span class="p">,</span> <span class="n">skip_this</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>
                    <span class="n">intf_func</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">=</span> <span class="n">pymodname</span>
                    <span class="n">interfaces</span><span class="p">[</span><span class="n">intf_name</span><span class="p">]</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">name</span><span class="p">,</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">fullname</span><span class="p">],</span> <span class="n">intf_func</span><span class="p">))</span>

            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    adding method </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
            <span class="k">del</span> <span class="n">routines</span><span class="p">[</span><span class="n">routines</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="n">fullname</span><span class="p">)]</span>

        <span class="c1"># only keep interfaces with more than one routine in them</span>
        <span class="k">for</span> <span class="n">name</span><span class="p">,</span><span class="n">value</span> <span class="ow">in</span> <span class="n">interfaces</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="n">orig_name</span> <span class="o">=</span> <span class="n">name</span>
            <span class="k">if</span> <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;finalise&#39;</span><span class="p">:</span> <span class="k">continue</span>
            <span class="k">if</span> <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;initialise&#39;</span> <span class="ow">or</span> \
                    <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="s1">&#39;initialise_ptr&#39;</span><span class="p">:</span> <span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;__init__&#39;</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">py_keywords_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">special_names</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">value</span><span class="p">)</span> <span class="o">&gt;</span> <span class="mi">1</span><span class="p">:</span>
                <span class="n">_interfaces</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># if there&#39;s just one routine we want to update the routine, prepending the interface docstring</span>
                <span class="n">rname</span><span class="p">,</span> <span class="n">rspec</span><span class="p">,</span> <span class="n">rfunc</span> <span class="o">=</span> <span class="n">value</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="n">fobj</span><span class="p">,</span> <span class="n">rspec</span> <span class="o">=</span> <span class="n">_routines</span><span class="p">[</span><span class="n">rname</span><span class="p">]</span>
                <span class="n">rspec</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;interfaces&#39;</span><span class="p">][</span><span class="n">orig_name</span><span class="p">][</span><span class="s1">&#39;doc&#39;</span><span class="p">])</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">rspec</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">]</span>

                <span class="k">del</span> <span class="n">_routines</span><span class="p">[</span><span class="n">rname</span><span class="p">]</span>
                <span class="n">_routines</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">fobj</span><span class="p">,</span> <span class="n">rspec</span><span class="p">)</span>
                <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;__init__&#39;</span><span class="p">:</span>
                    <span class="n">constructor</span> <span class="o">=</span> <span class="n">add_doc</span><span class="p">(</span><span class="n">wrapinit</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">fobj</span><span class="p">,</span> <span class="n">rspec</span><span class="p">,</span> <span class="n">rname</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span>
                                          <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;numpydoc&#39;</span><span class="p">,</span> <span class="n">skip_this</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>
                    <span class="fm">__init__</span> <span class="o">=</span> <span class="n">constructor</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">func</span> <span class="o">=</span> <span class="n">add_doc</span><span class="p">(</span><span class="n">wrapmethod</span><span class="p">(</span><span class="n">name</span><span class="p">),</span> <span class="n">fobj</span><span class="p">,</span> <span class="n">rspec</span><span class="p">,</span> <span class="n">rname</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span>
                                   <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;numpydoc&#39;</span><span class="p">,</span> <span class="n">skip_this</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>
                    <span class="n">func</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">=</span> <span class="n">pymodname</span>
                    <span class="n">methods</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">func</span>

        <span class="k">for</span> <span class="n">intf</span><span class="p">,</span> <span class="n">value</span> <span class="ow">in</span> <span class="n">_interfaces</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>

            <span class="c1"># if there&#39;s a routine with same name as interface, move it first</span>
            <span class="k">if</span> <span class="n">intf</span> <span class="ow">in</span> <span class="n">methods</span><span class="p">:</span>
                <span class="n">methods</span><span class="p">[</span><span class="s1">&#39;__&#39;</span><span class="o">+</span><span class="n">intf</span><span class="p">]</span> <span class="o">=</span> <span class="n">methods</span><span class="p">[</span><span class="n">intf</span><span class="p">]</span>

            <span class="n">docname</span> <span class="o">=</span> <span class="n">intf</span>
            <span class="k">if</span> <span class="n">docname</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;_&#39;</span><span class="p">):</span> <span class="n">docname</span><span class="o">=</span><span class="n">docname</span><span class="p">[:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>

            <span class="n">func</span> <span class="o">=</span> <span class="n">wrapinterface</span><span class="p">(</span><span class="n">intf</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">intf</span> <span class="o">==</span> <span class="s1">&#39;__init__&#39;</span><span class="p">:</span> <span class="n">docname</span> <span class="o">=</span> <span class="s1">&#39;initialise&#39;</span>
            <span class="n">docname</span> <span class="o">=</span> <span class="n">rev_special_names</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">docname</span><span class="p">,</span> <span class="n">docname</span><span class="p">)</span>

            <span class="n">doc</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;interfaces&#39;</span><span class="p">][</span><span class="n">docname</span><span class="p">][</span><span class="s1">&#39;doc&#39;</span><span class="p">])</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span>

            <span class="k">for</span> <span class="n">rname</span><span class="p">,</span><span class="n">spec</span><span class="p">,</span><span class="n">routine</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;args_str&#39;</span><span class="p">]:</span>
                    <span class="n">doc</span> <span class="o">+=</span> <span class="n">args_str_table</span><span class="p">(</span><span class="n">spec</span><span class="p">)</span>

            <span class="n">doc</span> <span class="o">+=</span> <span class="s1">&#39;Wrapper around Fortran interface ``</span><span class="si">%s</span><span class="s1">`` containing multiple routines:</span><span class="se">\n\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">intf</span>

            <span class="k">for</span> <span class="n">rname</span><span class="p">,</span><span class="n">spec</span><span class="p">,</span><span class="n">routine</span> <span class="ow">in</span> <span class="n">value</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">rname</span> <span class="ow">in</span> <span class="n">methods</span><span class="p">:</span>
                    <span class="n">methods</span><span class="p">[</span><span class="s1">&#39;_&#39;</span><span class="o">+</span><span class="n">rname</span><span class="p">]</span> <span class="o">=</span> <span class="n">methods</span><span class="p">[</span><span class="n">rname</span><span class="p">]</span>
                    <span class="k">del</span> <span class="n">methods</span><span class="p">[</span><span class="n">rname</span><span class="p">]</span>

                <span class="n">routine_lines</span> <span class="o">=</span> <span class="n">routine</span><span class="o">.</span><span class="vm">__doc__</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
                <span class="n">signature_line</span> <span class="o">=</span> <span class="n">routine_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
                <span class="k">if</span> <span class="n">intf</span> <span class="o">==</span> <span class="s1">&#39;__init__&#39;</span><span class="p">:</span>
                   <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="o">+</span><span class="n">rname</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">)</span>
                <span class="c1">#signature_line = signature_line.replace(rname, intf)</span>

                <span class="n">doc</span> <span class="o">+=</span>        <span class="p">(</span><span class="s1">&#39;   .. function :: </span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">signature_line</span> <span class="o">+</span>
                    <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s1">&#39;      </span><span class="si">%s</span><span class="s1">&#39;</span>   <span class="o">%</span> <span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">routine_lines</span><span class="p">[</span><span class="mi">1</span><span class="p">:]]))</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span>

            <span class="k">if</span> <span class="n">intf</span> <span class="o">!=</span> <span class="s1">&#39;__init__&#39;</span><span class="p">:</span>
                <span class="n">func</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">doc</span>
                <span class="n">func</span><span class="o">.</span><span class="vm">__module__</span> <span class="o">=</span> <span class="n">pymodname</span>
                <span class="n">methods</span><span class="p">[</span><span class="n">intf</span><span class="p">]</span> <span class="o">=</span> <span class="n">func</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">constructor</span> <span class="o">=</span> <span class="n">wrapinit</span><span class="p">(</span><span class="n">constructor_name</span><span class="p">,</span> <span class="n">doc</span><span class="p">)</span>

        <span class="n">constructor_doc_lines</span> <span class="o">=</span> <span class="n">constructor</span><span class="o">.</span><span class="vm">__doc__</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
        <span class="n">constructor_doc_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;Class is wrapper around Fortran type ``</span><span class="si">%s</span><span class="s1">`` defined in file :git:`</span><span class="si">%s</span><span class="s1">`.</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="bp">cls</span><span class="p">,</span> <span class="n">modfile</span><span class="p">))</span>

        <span class="n">classdoc</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">constructor_doc_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]]</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">+</span>
                             <span class="n">process_docstring</span><span class="p">(</span><span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;types&#39;</span><span class="p">][</span><span class="bp">cls</span><span class="p">][</span><span class="s1">&#39;doc&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">+</span>
                             <span class="p">[</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">]</span> <span class="o">+</span> <span class="n">constructor_doc_lines</span><span class="p">[</span><span class="mi">1</span><span class="p">:])</span>

        <span class="n">new_cls</span> <span class="o">=</span> <span class="nb">type</span><span class="p">(</span><span class="nb">object</span><span class="p">)(</span><span class="n">tcls</span><span class="p">,</span> <span class="p">(</span><span class="n">FortranDerivedType</span><span class="p">,),</span>
                              <span class="p">{</span><span class="s1">&#39;__doc__&#39;</span><span class="p">:</span> <span class="n">classdoc</span><span class="p">,</span>
                               <span class="s1">&#39;_moddoc&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                               <span class="s1">&#39;_modobj&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                               <span class="s1">&#39;_classdoc&#39;</span><span class="p">:</span> <span class="kc">None</span><span class="p">,</span>
                               <span class="s1">&#39;_routines&#39;</span><span class="p">:</span> <span class="n">_routines</span><span class="p">,</span>
                               <span class="s1">&#39;_subobjs&#39;</span><span class="p">:</span> <span class="p">{},</span>
                               <span class="s1">&#39;_arrays&#39;</span><span class="p">:</span> <span class="p">{},</span>
                               <span class="s1">&#39;_interfaces&#39;</span><span class="p">:</span> <span class="n">_interfaces</span><span class="p">,</span>
                               <span class="s1">&#39;_elements&#39;</span><span class="p">:</span> <span class="p">{},</span>
                               <span class="p">})</span>
        <span class="n">FortranDerivedTypes</span><span class="p">[</span><span class="s1">&#39;type(</span><span class="si">%s</span><span class="s1">)&#39;</span> <span class="o">%</span> <span class="bp">cls</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span> <span class="o">=</span> <span class="n">new_cls</span>
        <span class="n">classes</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">tcls</span><span class="p">,</span> <span class="n">new_cls</span><span class="p">))</span>

        <span class="n">new_cls</span><span class="o">.</span><span class="n">_classdoc</span> <span class="o">=</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;types&#39;</span><span class="p">][</span><span class="bp">cls</span><span class="p">]</span>
        <span class="n">new_cls</span><span class="o">.</span><span class="n">_moddoc</span> <span class="o">=</span> <span class="n">moddoc</span>
        <span class="n">new_cls</span><span class="o">.</span><span class="n">_modobj</span> <span class="o">=</span> <span class="n">modobj</span>
        <span class="n">new_cls</span><span class="o">.</span><span class="n">_prefix</span> <span class="o">=</span> <span class="n">prefix</span>

        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">func</span> <span class="ow">in</span> <span class="n">methods</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="nb">setattr</span><span class="p">(</span><span class="n">new_cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">func</span><span class="p">)</span>
        <span class="n">new_cls</span><span class="o">.</span><span class="fm">__init__</span> <span class="o">=</span> <span class="n">constructor</span>

        <span class="c1"># Add properties for get and set routines of scalar type, sub</span>
        <span class="c1"># objects, and arrays.</span>

        <span class="k">for</span> <span class="n">name</span><span class="p">,</span> <span class="n">el</span> <span class="ow">in</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;types&#39;</span><span class="p">][</span><span class="bp">cls</span><span class="p">][</span><span class="s1">&#39;elements&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
            <span class="k">if</span> <span class="n">name</span> <span class="o">==</span> <span class="s1">&#39;thetype&#39;</span><span class="p">:</span> <span class="n">name</span> <span class="o">=</span> <span class="s1">&#39;type&#39;</span> <span class="c1"># special case: f2py misparses name &#39;type&#39;</span>
            <span class="n">name</span> <span class="o">=</span> <span class="n">name</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="c1"># lower case all attributes</span>

            <span class="k">if</span> <span class="s1">&#39;get&#39;</span> <span class="ow">in</span> <span class="n">el</span> <span class="ow">and</span> <span class="s1">&#39;set&#39;</span> <span class="ow">in</span> <span class="n">el</span><span class="p">:</span>
                <span class="k">if</span> <span class="n">is_scalar_type</span><span class="p">(</span><span class="n">el</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]):</span>
                    <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    adding scalar property </span><span class="si">%s</span><span class="s1"> get=</span><span class="si">%s</span><span class="s1"> set=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;get&#39;</span><span class="p">],</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;set&#39;</span><span class="p">]))</span>

                    <span class="n">new_cls</span><span class="o">.</span><span class="n">_elements</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;get&#39;</span><span class="p">]),</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;set&#39;</span><span class="p">]),</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">])</span>

                    <span class="c1"># If element clashes with name of routine, move routine first</span>
                    <span class="k">if</span> <span class="nb">hasattr</span><span class="p">(</span><span class="n">new_cls</span><span class="p">,</span> <span class="n">name</span><span class="p">):</span>
                        <span class="nb">setattr</span><span class="p">(</span><span class="n">new_cls</span><span class="p">,</span> <span class="n">name</span><span class="o">+</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">new_cls</span><span class="p">,</span> <span class="n">name</span><span class="p">))</span>

                    <span class="nb">setattr</span><span class="p">(</span><span class="n">new_cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="nb">property</span><span class="p">(</span><span class="n">fget</span><span class="o">=</span><span class="n">wrap_get</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
                                                    <span class="n">fset</span><span class="o">=</span><span class="n">wrap_set</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
                                                    <span class="n">doc</span><span class="o">=</span><span class="n">process_docstring</span><span class="p">(</span><span class="n">el</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])))</span>
                <span class="c1">#elif not &#39;pointer&#39; in el[&#39;attributes&#39;]:</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    adding property </span><span class="si">%s</span><span class="s1"> get=</span><span class="si">%s</span><span class="s1"> set=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;get&#39;</span><span class="p">],</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;set&#39;</span><span class="p">]))</span>
                    <span class="n">new_cls</span><span class="o">.</span><span class="n">_subobjs</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="n">el</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">],</span>
                                              <span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;get&#39;</span><span class="p">]),</span>
                                              <span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;set&#39;</span><span class="p">]))</span>
                    <span class="nb">setattr</span><span class="p">(</span><span class="n">new_cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="nb">property</span><span class="p">(</span><span class="n">fget</span><span class="o">=</span><span class="n">wrap_obj_get</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
                                                    <span class="n">fset</span><span class="o">=</span><span class="n">wrap_obj_set</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
                                                    <span class="n">doc</span><span class="o">=</span><span class="n">process_docstring</span><span class="p">(</span><span class="n">el</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])))</span>


            <span class="c1"># array members</span>
            <span class="k">if</span> <span class="s1">&#39;array&#39;</span> <span class="ow">in</span> <span class="n">el</span><span class="p">:</span>
                <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    adding array </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
                <span class="n">new_cls</span><span class="o">.</span><span class="n">_arrays</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;array&#39;</span><span class="p">]),</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">],</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">])</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">new_cls</span><span class="p">,</span> <span class="s1">&#39;_&#39;</span><span class="o">+</span><span class="n">name</span><span class="p">,</span> <span class="nb">property</span><span class="p">(</span><span class="n">fget</span><span class="o">=</span><span class="n">wrap_array_get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="n">reshape</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span>
                                                    <span class="n">fset</span><span class="o">=</span><span class="n">wrap_array_set</span><span class="p">(</span><span class="n">name</span><span class="p">,</span><span class="n">reshape</span><span class="o">=</span><span class="kc">False</span><span class="p">),</span>
                                                    <span class="n">doc</span><span class="o">=</span><span class="n">process_docstring</span><span class="p">(</span><span class="n">el</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])))</span>
                <span class="nb">setattr</span><span class="p">(</span><span class="n">new_cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="nb">property</span><span class="p">(</span><span class="n">fget</span><span class="o">=</span><span class="n">wrap_array_get</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
                                                <span class="n">fset</span><span class="o">=</span><span class="n">wrap_array_set</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
                                                <span class="n">doc</span><span class="o">=</span><span class="n">process_docstring</span><span class="p">(</span><span class="n">el</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])))</span>

            <span class="c1"># arrays of derived types (1D only for now..)</span>
            <span class="k">if</span> <span class="s1">&#39;array_getitem&#39;</span> <span class="ow">in</span> <span class="n">el</span> <span class="ow">and</span> <span class="s1">&#39;array_setitem&#39;</span> <span class="ow">and</span> <span class="s1">&#39;array_len&#39;</span> <span class="ow">in</span> <span class="n">el</span><span class="p">:</span>
                <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;    adding derived type array </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
                <span class="n">new_cls</span><span class="o">.</span><span class="n">_arrays</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="p">(</span><span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;array_getitem&#39;</span><span class="p">]),</span>
                                         <span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;array_setitem&#39;</span><span class="p">]),</span>
                                         <span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;array_len&#39;</span><span class="p">]),</span>
                                         <span class="n">el</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">],</span> <span class="n">el</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">])</span>

                <span class="nb">setattr</span><span class="p">(</span><span class="n">new_cls</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="nb">property</span><span class="p">(</span><span class="n">fget</span><span class="o">=</span><span class="n">wrap_derived_type_array_get</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
                                                <span class="n">fset</span><span class="o">=</span><span class="n">wrap_derived_type_array_set</span><span class="p">(</span><span class="n">name</span><span class="p">),</span>
                                                <span class="n">doc</span><span class="o">=</span><span class="n">process_docstring</span><span class="p">(</span><span class="n">el</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])))</span>





    <span class="c1"># Try to evaluate params</span>
    <span class="n">evaldict</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="vm">__dict__</span>
    <span class="n">evaldict</span><span class="p">[</span><span class="s1">&#39;huge&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">:</span> <span class="n">np</span><span class="o">.</span><span class="n">finfo</span><span class="p">(</span><span class="s1">&#39;d&#39;</span><span class="p">)</span><span class="o">.</span><span class="n">max</span>
    <span class="n">evaldict</span><span class="p">[</span><span class="s1">&#39;cmplx&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="k">lambda</span> <span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="n">kind</span><span class="p">:</span> <span class="nb">complex</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span>
    <span class="n">evaldict</span><span class="p">[</span><span class="s1">&#39;farray&#39;</span><span class="p">]</span> <span class="o">=</span> <span class="n">farray</span>
    <span class="n">mod_params</span> <span class="o">=</span> <span class="p">{}</span>
    <span class="k">for</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">ftype</span><span class="p">,</span> <span class="n">attributes</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span> <span class="ow">in</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;parameters&#39;</span><span class="p">]:</span>
        <span class="n">code</span> <span class="o">=</span> <span class="n">value</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;(/&#39;</span><span class="p">,</span><span class="s1">&#39;farray([&#39;</span><span class="p">)</span>
        <span class="n">code</span> <span class="o">=</span> <span class="n">code</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;/)&#39;</span><span class="p">,</span><span class="s1">&#39;])&#39;</span><span class="p">)</span>
        <span class="n">code</span> <span class="o">=</span> <span class="n">code</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;_dp&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">mod_params</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="nb">eval</span><span class="p">(</span><span class="n">code</span><span class="p">,</span> <span class="n">evaldict</span><span class="p">)</span>
            <span class="n">evaldict</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="o">=</span> <span class="n">mod_params</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  adding parameter </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>
        <span class="k">except</span> <span class="p">(</span><span class="ne">NameError</span><span class="p">,</span> <span class="ne">IndexError</span><span class="p">):</span>
            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;  ignorning exception in parameter </span><span class="si">%s</span><span class="s1"> = </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">code</span><span class="p">))</span>

    <span class="k">return</span> <span class="p">(</span><span class="n">classes</span><span class="p">,</span> <span class="n">routines</span><span class="p">,</span> <span class="n">mod_params</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">wrap_get</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> object not initialised.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_elements</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown element </span><span class="si">%s</span><span class="s1"> in class </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class</span><span class="o">.</span><span class="n">__name</span><span class="p">))</span>
        <span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_elements</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="mi">0</span><span class="p">](</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_elements</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;logical&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">res</span> <span class="o">!=</span> <span class="n">QUIPPY_FALSE</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="n">res</span>

    <span class="k">return</span> <span class="n">func</span>

<span class="k">def</span> <span class="nf">wrap_set</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> object not initialised.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">name</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">_elements</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unknown element </span><span class="si">%s</span><span class="s1"> in class </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="bp">self</span><span class="o">.</span><span class="n">__class</span><span class="o">.</span><span class="n">__name</span><span class="p">))</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_elements</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="mi">2</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;logical&#39;</span><span class="p">:</span>
            <span class="n">value</span> <span class="o">=</span> <span class="n">value</span> <span class="ow">and</span> <span class="n">QUIPPY_TRUE</span> <span class="ow">or</span> <span class="n">QUIPPY_FALSE</span>

        <span class="n">res</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_elements</span><span class="p">[</span><span class="n">name</span><span class="p">][</span><span class="mi">1</span><span class="p">](</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">,</span> <span class="n">value</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_update</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">res</span>

    <span class="k">return</span> <span class="n">func</span>

<span class="k">def</span> <span class="nf">wrap_obj_get</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> object not initialised.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
        <span class="bp">cls</span><span class="p">,</span> <span class="n">getfunc</span><span class="p">,</span> <span class="n">setfunc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subobjs</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
        <span class="n">p</span> <span class="o">=</span> <span class="n">getfunc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">)</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="p">(</span><span class="n">p</span> <span class="o">==</span> <span class="mi">0</span><span class="p">)</span><span class="o">.</span><span class="n">all</span><span class="p">():</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subobjs_cache</span><span class="p">[</span><span class="nb">tuple</span><span class="p">(</span><span class="n">p</span><span class="p">)]</span>
            <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
                <span class="n">obj</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subobjs_cache</span><span class="p">[</span><span class="nb">tuple</span><span class="p">(</span><span class="n">p</span><span class="p">)]</span> <span class="o">=</span> <span class="n">FortranDerivedTypes</span><span class="p">[</span><span class="bp">cls</span><span class="o">.</span><span class="n">lower</span><span class="p">()](</span><span class="n">fpointer</span><span class="o">=</span><span class="n">p</span><span class="p">,</span><span class="n">finalise</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
                <span class="n">obj</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">proxy</span><span class="p">(</span><span class="bp">self</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">obj</span>

    <span class="k">return</span> <span class="n">func</span>

<span class="k">def</span> <span class="nf">wrap_obj_set</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> object not initialised.&#39;</span> <span class="o">%</span> <span class="bp">self</span><span class="o">.</span><span class="vm">__class__</span><span class="o">.</span><span class="vm">__name__</span><span class="p">)</span>
        <span class="bp">cls</span><span class="p">,</span> <span class="n">getfunc</span><span class="p">,</span> <span class="n">setfunc</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_subobjs</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
        <span class="n">setfunc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">,</span> <span class="n">value</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">func</span>

<span class="k">def</span> <span class="nf">wrap_array_get</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">reshape</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">arrayfunc</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">arraytype</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_arrays</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="n">arrayfunc</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">arraytype</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_arrays</span><span class="p">[</span><span class="s1">&#39;_&#39;</span><span class="o">+</span><span class="n">name</span><span class="p">]</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">a</span> <span class="o">=</span> <span class="n">arraydata</span><span class="o">.</span><span class="n">get_array</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">,</span> <span class="n">arrayfunc</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">get_fortran_indexing</span><span class="p">():</span>
                <span class="n">a</span> <span class="o">=</span> <span class="n">FortranArray</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">parent</span><span class="o">=</span><span class="bp">self</span><span class="p">)</span>
            <span class="n">nshape</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_array_shape</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">reshape</span> <span class="ow">and</span> <span class="n">nshape</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">a</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="n">nshape</span><span class="p">]</span>
            <span class="k">return</span> <span class="n">a</span>
        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>

    <span class="k">return</span> <span class="n">func</span>

<span class="k">def</span> <span class="nf">wrap_array_set</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">reshape</span><span class="o">=</span><span class="kc">True</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">arrayfunc</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">arraytype</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_arrays</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="n">arrayfunc</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">arraytype</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_arrays</span><span class="p">[</span><span class="s1">&#39;_&#39;</span><span class="o">+</span><span class="n">name</span><span class="p">]</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">a</span> <span class="o">=</span> <span class="n">arraydata</span><span class="o">.</span><span class="n">get_array</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">,</span> <span class="n">arrayfunc</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">get_fortran_indexing</span><span class="p">:</span>
                <span class="n">a</span> <span class="o">=</span> <span class="n">FortranArray</span><span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">doc</span><span class="p">)</span>
            <span class="n">nshape</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_get_array_shape</span><span class="p">(</span><span class="n">name</span><span class="p">)</span>
            <span class="k">if</span> <span class="n">reshape</span> <span class="ow">and</span> <span class="n">nshape</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
                <span class="n">a</span> <span class="o">=</span> <span class="n">a</span><span class="p">[</span><span class="n">nshape</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">arraytype</span> <span class="o">==</span> <span class="s1">&#39;logical&#39;</span><span class="p">:</span>
                <span class="n">value</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">where</span><span class="p">(</span><span class="n">value</span><span class="p">,</span> <span class="n">QUIPPY_TRUE</span><span class="p">,</span> <span class="n">QUIPPY_FALSE</span><span class="p">)</span>
            <span class="n">a</span><span class="p">[</span><span class="o">...</span><span class="p">]</span> <span class="o">=</span> <span class="n">value</span>

        <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">None</span>

    <span class="k">return</span> <span class="n">func</span>

<span class="k">def</span> <span class="nf">wrap_derived_type_array_get</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="n">getfunc</span><span class="p">,</span> <span class="n">setfunc</span><span class="p">,</span> <span class="n">lenfunc</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">arraytype</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_arrays</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">FortranDerivedTypeArray</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">getfunc</span><span class="p">,</span> <span class="n">setfunc</span><span class="p">,</span>
                                       <span class="n">lenfunc</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">arraytype</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">func</span>

<span class="k">def</span> <span class="nf">wrap_derived_type_array_set</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="n">getfunc</span><span class="p">,</span> <span class="n">setfunc</span><span class="p">,</span> <span class="n">lenfunc</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">arraytype</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_arrays</span><span class="p">[</span><span class="n">name</span><span class="p">]</span>
        <span class="n">a</span> <span class="o">=</span> <span class="n">FortranDerivedTypeArray</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">getfunc</span><span class="p">,</span> <span class="n">setfunc</span><span class="p">,</span>
                                    <span class="n">lenfunc</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">arraytype</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">v</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">a</span><span class="o">.</span><span class="n">indices</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
            <span class="n">a</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">v</span>

    <span class="k">return</span> <span class="n">func</span>

<span class="k">class</span> <span class="nc">FortranDerivedTypeArray</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">parent</span><span class="p">,</span> <span class="n">getfunc</span><span class="p">,</span> <span class="n">setfunc</span><span class="p">,</span> <span class="n">lenfunc</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">arraytype</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">parent</span> <span class="o">=</span> <span class="n">weakref</span><span class="o">.</span><span class="n">proxy</span><span class="p">(</span><span class="n">parent</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">getfunc</span> <span class="o">=</span> <span class="n">getfunc</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setfunc</span> <span class="o">=</span> <span class="n">setfunc</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">lenfunc</span> <span class="o">=</span> <span class="n">lenfunc</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">doc</span> <span class="o">=</span> <span class="n">doc</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">arraytype</span> <span class="o">=</span> <span class="n">arraytype</span>

    <span class="k">def</span> <span class="nf">iterindices</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">if</span> <span class="n">get_fortran_indexing</span><span class="p">():</span>
            <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="n">frange</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="p">)))</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="nb">iter</span><span class="p">(</span><span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="p">)))</span>

    <span class="n">indices</span> <span class="o">=</span> <span class="nb">property</span><span class="p">(</span><span class="n">iterindices</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">iteritems</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">for</span> <span class="n">idx</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">indices</span><span class="p">:</span>
            <span class="k">yield</span> <span class="bp">self</span><span class="p">[</span><span class="n">idx</span><span class="p">]</span>

    <span class="k">def</span> <span class="nf">__iter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">iteritems</span><span class="p">()</span>

    <span class="k">def</span> <span class="nf">__len__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">lenfunc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">)</span>

    <span class="k">def</span> <span class="nf">__getitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">get_fortran_indexing</span><span class="p">():</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="n">pp</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">getfunc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">_subobjs_cache</span><span class="p">[</span><span class="nb">tuple</span><span class="p">(</span><span class="n">pp</span><span class="p">)]</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="n">obj</span> <span class="o">=</span> <span class="n">p</span><span class="o">.</span><span class="n">_subobjs_cache</span><span class="p">[</span><span class="nb">tuple</span><span class="p">(</span><span class="n">pp</span><span class="p">)]</span> <span class="o">=</span> <span class="n">FortranDerivedTypes</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">arraytype</span><span class="o">.</span><span class="n">lower</span><span class="p">()](</span><span class="n">fpointer</span><span class="o">=</span><span class="n">pp</span><span class="p">,</span><span class="n">finalise</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
        <span class="k">return</span> <span class="n">obj</span>

    <span class="k">def</span> <span class="nf">__setitem__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">value</span><span class="p">):</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="n">get_fortran_indexing</span><span class="p">():</span>
            <span class="n">i</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">setfunc</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">parent</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">,</span> <span class="n">i</span><span class="p">,</span> <span class="n">value</span><span class="o">.</span><span class="n">_fpointer</span><span class="p">)</span>


<span class="c1"># regexp magic to convert old f90doc documentation syntax to something more compatible with ResT</span>
<span class="n">doc_subs</span> <span class="o">=</span> <span class="p">[(</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s1">&#39;([A-Za-z0-9_]+)%([A-Za-z0-9_]+)&#39;</span><span class="p">),</span> <span class="sa">r</span><span class="s1">&#39;\1.\2&#39;</span><span class="p">),</span> <span class="c1"># Fortran attribute access % -&gt; Python .</span>
            <span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;&#39;(.*?)&#39;&quot;</span><span class="p">),</span> <span class="sa">r</span><span class="s2">&quot;``\1``&quot;</span><span class="p">),</span> <span class="c1"># Single quoted strings to fixed width font</span>
            <span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;(\w+)_(\W)&quot;</span><span class="p">),</span> <span class="sa">r</span><span class="s2">&quot;\1\_\2&quot;</span><span class="p">),</span> <span class="c1"># Escape underscores at the end of words...</span>
            <span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\$(.*?)(</span><span class="se">\\</span><span class="s2">_)(.*?)\$&quot;</span><span class="p">),</span> <span class="sa">r</span><span class="s1">&#39;$\1_\3$&#39;</span><span class="p">),</span> <span class="c1"># ... except in math mode</span>
            <span class="p">(</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="sa">r</span><span class="s2">&quot;\$(.*?)\$&quot;</span><span class="p">),</span> <span class="sa">r</span><span class="s2">&quot;:math:`\1`&quot;</span><span class="p">)</span> <span class="c1"># LaTeX $...$ -&gt; :math:`...`</span>
            <span class="p">]</span>

<span class="k">def</span> <span class="nf">normalise_type_case</span><span class="p">(</span><span class="nb">type</span><span class="p">):</span>
    <span class="n">classnames</span> <span class="o">=</span> <span class="p">[</span><span class="n">c</span><span class="o">.</span><span class="vm">__name__</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">FortranDerivedTypes</span><span class="o">.</span><span class="n">values</span><span class="p">()]</span>
    <span class="n">lower_to_normed</span> <span class="o">=</span> <span class="nb">dict</span><span class="p">((</span><span class="n">c</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="n">c</span><span class="p">)</span> <span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">classnames</span><span class="p">)</span>
    <span class="k">return</span> <span class="n">lower_to_normed</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="nb">type</span><span class="o">.</span><span class="n">lower</span><span class="p">(),</span> <span class="nb">type</span><span class="p">)</span>

<span class="k">def</span> <span class="nf">process_docstring</span><span class="p">(</span><span class="n">doc</span><span class="p">):</span>
    <span class="c1"># turn verbatim and displayed maths sections into Sphinx blockquotes</span>
    <span class="n">indent</span> <span class="o">=</span> <span class="mi">0</span>
    <span class="n">lines</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="n">in_verbatim</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">in_math</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">in_literal</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="n">first_line_in_verbatim</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">)):</span>
        <span class="k">if</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;&gt;&#39;</span><span class="p">):</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="n">in_verbatim</span><span class="p">:</span>
                <span class="n">in_verbatim</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">first_line_in_verbatim</span> <span class="o">=</span> <span class="kc">True</span>
                <span class="n">indent</span> <span class="o">+=</span> <span class="mi">3</span>
                <span class="k">if</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
                    <span class="k">if</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">):</span>
                        <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="s1">&#39;:</span><span class="se">\n</span><span class="s1">&#39;</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="s1">&#39; ::</span><span class="se">\n</span><span class="s1">&#39;</span>
                <span class="k">elif</span> <span class="ow">not</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;::&#39;</span><span class="p">):</span>
                    <span class="k">if</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">):</span>
                        <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="o">+=</span> <span class="s1">&#39;:&#39;</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">2</span><span class="p">]</span> <span class="o">+=</span> <span class="s1">&#39; ::&#39;</span>
            <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;&gt;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="k">elif</span> <span class="n">in_verbatim</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">first_line_in_verbatim</span><span class="p">:</span>
                <span class="n">first_line_in_verbatim</span> <span class="o">=</span> <span class="kc">False</span>
                <span class="k">if</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">!=</span> <span class="s1">&#39;&#39;</span><span class="p">:</span>
                    <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">+=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>
            <span class="n">in_verbatim</span> <span class="o">=</span> <span class="kc">False</span>
            <span class="n">indent</span> <span class="o">-=</span> <span class="mi">3</span>

        <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;</span> <span class="mi">1</span> <span class="ow">and</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s1">&#39;::&#39;</span><span class="p">)</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">in_literal</span><span class="p">:</span>
            <span class="n">literal_start_col</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">])</span> <span class="o">-</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">lstrip</span><span class="p">())</span>
            <span class="n">in_literal</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="k">if</span> <span class="n">in_literal</span> <span class="ow">and</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
            <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">&gt;</span> <span class="n">literal_start_col</span> <span class="ow">and</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">][</span><span class="n">literal_start_col</span><span class="p">]</span> <span class="o">!=</span> <span class="s1">&#39; &#39;</span><span class="p">:</span>
                <span class="n">in_literal</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">begin</span><span class="si">{displaymath}</span><span class="s1">&#39;</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
            <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">begin</span><span class="si">{displaymath}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">.. math::</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
            <span class="n">indent</span> <span class="o">+=</span> <span class="mi">3</span>
            <span class="n">in_math</span> <span class="o">=</span> <span class="kc">True</span>

        <span class="k">if</span> <span class="s1">&#39;</span><span class="se">\\</span><span class="s1">end</span><span class="si">{displaymath}</span><span class="s1">&#39;</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]:</span>
            <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\\</span><span class="s1">end</span><span class="si">{displaymath}</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
            <span class="n">indent</span> <span class="o">-=</span> <span class="mi">3</span>
            <span class="n">in_math</span> <span class="o">=</span> <span class="kc">False</span>

        <span class="k">if</span> <span class="ow">not</span> <span class="n">in_verbatim</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">in_literal</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">in_math</span><span class="p">:</span>
            <span class="k">for</span> <span class="n">regexp</span><span class="p">,</span> <span class="n">repl</span> <span class="ow">in</span> <span class="n">doc_subs</span><span class="p">:</span>
                <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">regexp</span><span class="o">.</span><span class="n">sub</span><span class="p">(</span><span class="n">repl</span><span class="p">,</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>

        <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="s1">&#39; &#39;</span><span class="o">*</span><span class="n">indent</span> <span class="o">+</span> <span class="n">lines</span><span class="p">[</span><span class="n">i</span><span class="p">]</span>

    <span class="k">try</span><span class="p">:</span>
       <span class="n">doc</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">cleandoc</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lines</span><span class="p">))</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
       <span class="n">doc</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">doc</span>


<span class="k">def</span> <span class="nf">add_doc</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">fobj</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">fullname</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;numpydoc&#39;</span><span class="p">,</span> <span class="n">skip_this</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="n">func</span><span class="o">.</span><span class="vm">__name__</span> <span class="o">=</span> <span class="n">fullname</span>
    <span class="n">func</span><span class="o">.</span><span class="n">_fobj</span> <span class="o">=</span> <span class="n">fobj</span>

    <span class="k">if</span> <span class="n">doc</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">func</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="k">return</span> <span class="n">func</span>

    <span class="k">if</span> <span class="nb">format</span> <span class="ow">not</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">&#39;numpydoc&#39;</span><span class="p">,</span> <span class="s1">&#39;sphinx&#39;</span><span class="p">]:</span>
        <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;Unsuported format </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="nb">format</span><span class="p">)</span>

    <span class="k">if</span> <span class="nb">format</span> <span class="o">==</span> <span class="s1">&#39;numpydoc&#39;</span><span class="p">:</span>
        <span class="n">arg_header</span> <span class="o">=</span> <span class="s1">&#39;Parameters</span><span class="se">\n</span><span class="s1">----------</span><span class="se">\n</span><span class="s1">&#39;</span>
        <span class="n">ret_header</span> <span class="o">=</span> <span class="s1">&#39;Returns</span><span class="se">\n</span><span class="s1">-------</span><span class="se">\n</span><span class="s1">&#39;</span>
        <span class="n">ref_header</span> <span class="o">=</span> <span class="s1">&#39;References</span><span class="se">\n</span><span class="s1">----------</span><span class="se">\n</span><span class="s1">&#39;</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">arg_header</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="n">ret_header</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="n">ref_header</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>

    <span class="n">d</span> <span class="o">=</span> <span class="n">fobj</span><span class="o">.</span><span class="vm">__doc__</span>
    <span class="n">L</span> <span class="o">=</span> <span class="n">d</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>
    <span class="n">arg_lines_in</span> <span class="o">=</span> <span class="n">L</span><span class="p">[</span><span class="mi">3</span><span class="p">:]</span>

    <span class="n">arg_lines</span> <span class="o">=</span> <span class="p">[]</span>
    <span class="n">ret_lines</span> <span class="o">=</span> <span class="p">[]</span>

    <span class="n">signature_line</span> <span class="o">=</span> <span class="n">L</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>

    <span class="k">if</span> <span class="s1">&#39;.&#39;</span> <span class="ow">in</span> <span class="n">name</span><span class="p">:</span>
        <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">name</span><span class="p">[:</span><span class="n">name</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;.&#39;</span><span class="p">)]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">+</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
    <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">prefix</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="s1">&#39;=&#39;</span> <span class="ow">in</span> <span class="n">signature_line</span><span class="p">:</span>
        <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="p">[:</span><span class="n">signature_line</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="p">)]</span> <span class="o">+</span> <span class="n">signature_line</span><span class="p">[</span><span class="n">signature_line</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="p">):]</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">fullname</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>
        <span class="n">retvar</span> <span class="o">=</span> <span class="n">signature_line</span><span class="p">[:</span><span class="n">signature_line</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="p">)]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="p">[</span><span class="n">signature_line</span><span class="o">.</span><span class="n">index</span><span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="p">)</span><span class="o">+</span><span class="mi">1</span><span class="p">:]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">fullname</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span><span class="o">+</span><span class="s1">&#39;_&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="n">retvar</span> <span class="o">=</span> <span class="kc">None</span>
        <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">fullname</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">skip_this</span><span class="p">:</span>
        <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;this,&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>
        <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;this[,&#39;</span><span class="p">,</span> <span class="s1">&#39;[&#39;</span><span class="p">)</span>
        <span class="n">signature_line</span> <span class="o">=</span> <span class="n">signature_line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;(this)&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">)</span>


    <span class="n">final_doc</span> <span class="o">=</span> <span class="n">signature_line</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span>

    <span class="k">if</span> <span class="n">doc</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">]:</span>
        <span class="n">final_doc</span> <span class="o">+=</span> <span class="n">process_docstring</span><span class="p">(</span><span class="n">doc</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span>

    <span class="n">got_args_str</span> <span class="o">=</span> <span class="kc">False</span>
    <span class="k">if</span> <span class="n">arg_lines_in</span><span class="p">:</span>
        <span class="k">for</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">doc</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">]:</span>
            <span class="n">argname</span> <span class="o">=</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">argname</span> <span class="o">==</span> <span class="s1">&#39;args_str&#39;</span><span class="p">:</span>
                <span class="n">got_args_str</span> <span class="o">=</span> <span class="kc">True</span>
            <span class="k">if</span> <span class="n">argname</span> <span class="ow">in</span> <span class="n">badnames</span><span class="p">:</span> <span class="n">argname</span> <span class="o">=</span> <span class="n">badnames</span><span class="p">[</span><span class="n">argname</span><span class="p">]</span>

            <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">arg_lines_in</span><span class="p">):</span>
                <span class="k">if</span> <span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> :&#39;</span> <span class="o">%</span> <span class="n">argname</span><span class="p">):</span> <span class="k">break</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> not found in lines </span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">argname</span><span class="p">,</span> <span class="n">arg_lines_in</span><span class="p">))</span>

            <span class="n">line</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">argname</span><span class="p">,</span><span class="n">argname</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">prefix</span><span class="p">):])</span>
            <span class="n">argname</span><span class="p">,</span> <span class="nb">type</span> <span class="o">=</span> <span class="n">line</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;:&#39;</span><span class="p">,</span> <span class="mi">2</span><span class="p">)</span>
            <span class="n">argname</span> <span class="o">=</span> <span class="n">argname</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
            <span class="k">if</span> <span class="n">argname</span> <span class="o">==</span> <span class="s1">&#39;this&#39;</span> <span class="ow">and</span> <span class="n">skip_this</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="k">if</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;type(&#39;</span><span class="p">):</span>
                <span class="nb">type</span> <span class="o">=</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">][</span><span class="mi">5</span><span class="p">:</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span>
                <span class="nb">type</span> <span class="o">=</span> <span class="n">normalise_type_case</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span>
                <span class="nb">type</span> <span class="o">=</span> <span class="s1">&#39;:class:`~.</span><span class="si">%s</span><span class="s1">` object&#39;</span> <span class="o">%</span> <span class="nb">type</span>
            <span class="k">if</span> <span class="s1">&#39;optional&#39;</span> <span class="ow">in</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]:</span>
                <span class="nb">type</span> <span class="o">+=</span> <span class="s1">&#39;, optional&#39;</span>


            <span class="k">if</span> <span class="n">argname</span> <span class="o">==</span> <span class="n">retvar</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">format</span> <span class="o">==</span> <span class="s1">&#39;numpydoc&#39;</span><span class="p">:</span>
                    <span class="n">ret_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> : </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">argname</span><span class="p">,</span> <span class="nb">type</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">]:</span>
                        <span class="n">ret_lines</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s1">&#39;    &#39;</span> <span class="o">+</span> <span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">process_docstring</span><span class="p">(</span><span class="n">arg</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span> <span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">ret_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;:returns: **</span><span class="si">%s</span><span class="s1">** -- </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">argname</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">]))</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">format</span> <span class="o">==</span> <span class="s1">&#39;numpydoc&#39;</span><span class="p">:</span>
                    <span class="n">arg_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> : </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">argname</span><span class="p">,</span> <span class="nb">type</span><span class="p">))</span>
                    <span class="k">if</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">]:</span>
                        <span class="n">arg_lines</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s1">&#39;    &#39;</span> <span class="o">+</span> <span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">process_docstring</span><span class="p">(</span><span class="n">arg</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span> <span class="p">])</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">arg_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;:param </span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">argname</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">process_docstring</span><span class="p">(</span><span class="n">arg</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">))))</span>
                    <span class="k">if</span> <span class="nb">type</span><span class="p">:</span>
                        <span class="n">arg_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="s1">&#39;:type </span><span class="si">%s</span><span class="s1">: </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">argname</span><span class="p">,</span> <span class="nb">type</span><span class="p">))</span>

    <span class="k">if</span> <span class="nb">format</span> <span class="o">!=</span> <span class="s1">&#39;sphinx&#39;</span><span class="p">:</span>
        <span class="n">final_doc</span> <span class="o">+=</span> <span class="n">args_str_table</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">arg_lines</span><span class="p">:</span>
        <span class="n">final_doc</span> <span class="o">+=</span> <span class="n">arg_header</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">arg_lines</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span>
    <span class="k">if</span> <span class="n">ret_lines</span><span class="p">:</span>
        <span class="n">final_doc</span> <span class="o">+=</span> <span class="n">ret_header</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">ret_lines</span><span class="p">)</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span>

    <span class="n">final_doc</span> <span class="o">+=</span> <span class="n">ref_header</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">Routine is wrapper around Fortran routine ``</span><span class="si">%s</span><span class="s1">`` defined in file :git:`</span><span class="si">%s</span><span class="s1">`.&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">fullname</span><span class="p">,</span> <span class="n">modfile</span><span class="p">)</span>

    <span class="n">func</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">final_doc</span>
    <span class="k">return</span> <span class="n">func</span>


<span class="k">def</span> <span class="nf">args_str_table</span><span class="p">(</span><span class="n">spec</span><span class="p">):</span>
    <span class="k">if</span> <span class="ow">not</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;args_str&#39;</span><span class="p">]:</span>
        <span class="k">return</span> <span class="s1">&#39;&#39;</span>

    <span class="n">value_map</span> <span class="o">=</span> <span class="p">{</span><span class="s1">&#39;T&#39;</span><span class="p">:</span> <span class="s1">&#39;True&#39;</span><span class="p">,</span>
                 <span class="s1">&#39;F&#39;</span><span class="p">:</span> <span class="s1">&#39;False&#39;</span><span class="p">}</span>

    <span class="n">names</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Name&#39;</span><span class="p">]</span>
    <span class="n">types</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Type&#39;</span><span class="p">]</span>
    <span class="n">defaults</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Default&#39;</span><span class="p">]</span>
    <span class="n">docs</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Comments&#39;</span><span class="p">]</span>

    <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="nb">sorted</span><span class="p">(</span><span class="n">spec</span><span class="p">[</span><span class="s1">&#39;args_str&#39;</span><span class="p">]</span><span class="o">.</span><span class="n">keys</span><span class="p">()):</span>
        <span class="n">arg</span> <span class="o">=</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;args_str&#39;</span><span class="p">][</span><span class="n">name</span><span class="p">]</span>
        <span class="n">default</span> <span class="o">=</span> <span class="s1">&#39;None&#39;</span>
        <span class="k">if</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">]:</span>
            <span class="n">default</span> <span class="o">=</span> <span class="n">value_map</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">arg</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">],</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;value&#39;</span><span class="p">])</span>
        <span class="nb">type</span> <span class="o">=</span> <span class="n">python_equivalent_type</span><span class="p">(</span><span class="n">arg</span><span class="p">[</span><span class="s1">&#39;type&#39;</span><span class="p">])</span>
        <span class="n">doc</span> <span class="o">=</span> <span class="n">arg</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">]</span>

        <span class="n">names</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">name</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
        <span class="n">types</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="nb">type</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
        <span class="n">defaults</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">default</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
        <span class="n">doc</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
        <span class="n">doc</span> <span class="o">=</span> <span class="n">doc</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">capitalize</span><span class="p">()</span> <span class="o">+</span> <span class="n">doc</span><span class="p">[</span><span class="mi">1</span><span class="p">:]</span> <span class="c1"># capitalise first letter only</span>
        <span class="n">docs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>

    <span class="n">max_name_len</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">name</span><span class="p">)</span> <span class="k">for</span> <span class="n">name</span> <span class="ow">in</span> <span class="n">names</span><span class="p">)</span>
    <span class="n">max_type_len</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="nb">type</span><span class="p">)</span> <span class="k">for</span> <span class="nb">type</span> <span class="ow">in</span> <span class="n">types</span><span class="p">)</span>
    <span class="n">max_default_len</span> <span class="o">=</span> <span class="nb">max</span><span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">default</span><span class="p">)</span> <span class="k">for</span> <span class="n">default</span> <span class="ow">in</span> <span class="n">defaults</span><span class="p">)</span>

    <span class="n">cols</span> <span class="o">=</span> <span class="p">(</span><span class="n">max_name_len</span><span class="p">,</span> <span class="n">max_type_len</span><span class="p">,</span> <span class="n">max_default_len</span><span class="p">,</span> <span class="mi">40</span><span class="p">)</span>

    <span class="n">args_str_lines</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;.. rubric:: args_str options&#39;</span><span class="p">,</span><span class="s1">&#39;&#39;</span><span class="p">]</span>
    <span class="n">fmt</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="si">%%</span><span class="s1">-</span><span class="si">%d</span><span class="s1">s </span><span class="si">%%</span><span class="s1">-</span><span class="si">%d</span><span class="s1">s </span><span class="si">%%</span><span class="s1">-</span><span class="si">%d</span><span class="s1">s </span><span class="si">%%</span><span class="s1">-</span><span class="si">%d</span><span class="s1">s&#39;</span> <span class="o">%</span> <span class="n">cols</span>

    <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">default</span><span class="p">,</span> <span class="n">doc</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="nb">zip</span><span class="p">(</span><span class="n">names</span><span class="p">,</span> <span class="n">types</span><span class="p">,</span> <span class="n">defaults</span><span class="p">,</span> <span class="n">docs</span><span class="p">)):</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="n">args_str_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fmt</span> <span class="o">%</span> <span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="o">*</span><span class="n">cols</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s1">&#39;=&#39;</span><span class="o">*</span><span class="n">cols</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s1">&#39;=&#39;</span><span class="o">*</span><span class="n">cols</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="s1">&#39;=&#39;</span><span class="o">*</span><span class="n">cols</span><span class="p">[</span><span class="mi">3</span><span class="p">]))</span>
        <span class="n">doc_words</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">split</span><span class="p">()</span>
        <span class="k">while</span> <span class="n">doc_words</span><span class="p">:</span>
            <span class="n">doc_line</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
            <span class="k">while</span> <span class="n">doc_words</span><span class="p">:</span>
                <span class="n">word</span> <span class="o">=</span> <span class="n">doc_words</span><span class="o">.</span><span class="n">pop</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
                <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">doc_line</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">word</span><span class="p">)</span> <span class="o">&gt;</span> <span class="n">cols</span><span class="p">[</span><span class="mi">3</span><span class="p">]:</span>
                    <span class="n">doc_words</span><span class="o">.</span><span class="n">insert</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="n">word</span><span class="p">)</span> <span class="c1"># put it back</span>
                    <span class="k">break</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="n">doc_line</span> <span class="o">=</span> <span class="n">doc_line</span> <span class="o">+</span> <span class="s1">&#39; &#39;</span> <span class="o">+</span> <span class="n">word</span>

            <span class="n">args_str_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fmt</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="nb">type</span><span class="p">,</span> <span class="n">default</span><span class="p">,</span> <span class="n">doc_line</span><span class="o">.</span><span class="n">strip</span><span class="p">()))</span>
            <span class="n">name</span> <span class="o">=</span> <span class="nb">type</span> <span class="o">=</span> <span class="n">default</span> <span class="o">=</span> <span class="s1">&#39;&#39;</span>
        <span class="k">if</span> <span class="n">i</span> <span class="o">==</span> <span class="mi">0</span> <span class="ow">or</span> <span class="n">i</span> <span class="o">==</span> <span class="nb">len</span><span class="p">(</span><span class="n">names</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">:</span>
            <span class="n">args_str_lines</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">fmt</span> <span class="o">%</span> <span class="p">(</span><span class="s1">&#39;=&#39;</span><span class="o">*</span><span class="n">cols</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="s1">&#39;=&#39;</span><span class="o">*</span><span class="n">cols</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="s1">&#39;=&#39;</span><span class="o">*</span><span class="n">cols</span><span class="p">[</span><span class="mi">2</span><span class="p">],</span> <span class="s1">&#39;=&#39;</span><span class="o">*</span><span class="n">cols</span><span class="p">[</span><span class="mi">3</span><span class="p">]))</span>

    <span class="n">args_str_lines</span><span class="o">.</span><span class="n">extend</span><span class="p">([</span><span class="s1">&#39;&#39;</span><span class="p">,</span> <span class="s1">&#39;&#39;</span><span class="p">])</span>

    <span class="k">return</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">args_str_lines</span><span class="p">)</span>



<span class="k">def</span> <span class="nf">wraproutine</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">moddoc</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">shortname</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">skip_this</span><span class="o">=</span><span class="kc">False</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="n">doc</span> <span class="o">=</span> <span class="n">moddoc</span><span class="p">[</span><span class="s1">&#39;routines&#39;</span><span class="p">][</span><span class="n">name</span><span class="p">]</span>
    <span class="n">fobj</span> <span class="o">=</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">modobj</span><span class="p">,</span> <span class="n">prefix</span><span class="o">+</span><span class="n">name</span><span class="p">)</span>

    <span class="n">inargs</span>  <span class="o">=</span> <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">doc</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="ow">not</span> <span class="s1">&#39;intent(out)&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>
    <span class="n">outargs</span> <span class="o">=</span> <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">doc</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="s1">&#39;intent(out)&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>
    <span class="n">args_str_args</span> <span class="o">=</span> <span class="n">doc</span><span class="p">[</span><span class="s1">&#39;args_str&#39;</span><span class="p">]</span>

    <span class="c1">#inargs  = filter(lambda x: not &#39;intent(out)&#39; in x[&#39;attributes&#39;], doc[&#39;args&#39;])</span>
    <span class="c1">#outargs = filter(lambda x: &#39;intent(out)&#39; in x[&#39;attributes&#39;], doc[&#39;args&#39;])</span>

    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">newargs</span><span class="p">,</span> <span class="n">modargs</span><span class="p">,</span> <span class="n">newkwargs</span><span class="p">,</span> <span class="n">modkwargs</span> <span class="o">=</span> <span class="n">process_in_args</span><span class="p">(</span><span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="n">inargs</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">args_str_args</span><span class="p">,</span> <span class="n">name</span><span class="p">)</span>

        <span class="k">try</span><span class="p">:</span>
            <span class="n">res</span> <span class="o">=</span> <span class="n">fobj</span><span class="p">(</span><span class="o">*</span><span class="n">newargs</span><span class="p">,</span> <span class="o">**</span><span class="n">newkwargs</span><span class="p">)</span>
        <span class="k">except</span> <span class="ne">RuntimeError</span><span class="p">:</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="n">exctype</span><span class="p">,</span> <span class="n">value</span><span class="p">,</span> <span class="n">tb</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()</span>

                <span class="k">if</span> <span class="n">is_interactive_shell</span><span class="p">():</span>
                    <span class="n">error_str</span> <span class="o">=</span> <span class="s1">&#39;Fortran routine </span><span class="si">%s</span><span class="s1">(</span><span class="si">%s</span><span class="s1">):</span><span class="se">\n</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="s1">&#39;, &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="nb">repr</span><span class="p">(</span><span class="n">a</span><span class="p">)</span> <span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">args</span><span class="p">]</span> <span class="o">+</span> <span class="p">[</span><span class="s1">&#39;</span><span class="si">%s</span><span class="s1">=</span><span class="si">%r</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">k</span><span class="p">,</span><span class="n">v</span><span class="p">)</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">iteritems</span><span class="p">()]),</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">())</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># Remove Fortran traceback</span>
                    <span class="n">error_str</span> <span class="o">=</span> <span class="nb">str</span><span class="p">(</span><span class="n">value</span><span class="p">)</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>

                <span class="k">raise</span> <span class="n">exctype</span><span class="p">,</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="n">error_str</span><span class="p">),</span> <span class="n">tb</span>

            <span class="k">finally</span><span class="p">:</span>
                <span class="k">del</span> <span class="n">tb</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="k">raise</span>

        <span class="n">newres</span> <span class="o">=</span> <span class="n">process_results</span><span class="p">(</span><span class="n">res</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="n">kwargs</span><span class="p">,</span> <span class="n">inargs</span><span class="p">,</span> <span class="n">outargs</span><span class="p">,</span> <span class="n">prefix</span><span class="p">)</span>

        <span class="k">return</span> <span class="n">newres</span>

    <span class="k">return</span> <span class="n">add_doc</span><span class="p">(</span><span class="n">func</span><span class="p">,</span> <span class="n">fobj</span><span class="p">,</span> <span class="n">doc</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">shortname</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span>
                   <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;numpydoc&#39;</span><span class="p">,</span> <span class="n">skip_this</span><span class="o">=</span><span class="n">skip_this</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>


<span class="k">def</span> <span class="nf">wrapinterface</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">intf_spec</span><span class="p">,</span> <span class="n">routines</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>

    <span class="k">def</span> <span class="nf">func</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Interface </span><span class="si">%s</span><span class="s1"> &#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">rname</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">routine</span><span class="p">,</span> <span class="n">modfile</span> <span class="ow">in</span> <span class="n">routines</span><span class="p">:</span>
            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Trying candidate routine </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">rname</span><span class="p">)</span>

            <span class="n">inargs</span>  <span class="o">=</span> <span class="p">[</span> <span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">spec</span><span class="p">[</span><span class="s1">&#39;args&#39;</span><span class="p">]</span> <span class="k">if</span> <span class="ow">not</span> <span class="s1">&#39;intent(out)&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>
            <span class="n">oblig_inargs</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inargs</span> <span class="k">if</span>  <span class="ow">not</span> <span class="s1">&#39;optional&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>
            <span class="n">opt_inargs</span>   <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inargs</span> <span class="k">if</span> <span class="s1">&#39;optional&#39;</span> <span class="ow">in</span> <span class="n">x</span><span class="p">[</span><span class="s1">&#39;attributes&#39;</span><span class="p">]</span> <span class="p">]</span>

            <span class="c1">#inargs = filter(lambda x: not &#39;intent(out)&#39; in x[&#39;attributes&#39;], spec[&#39;args&#39;])</span>
            <span class="c1">#oblig_inargs = filter(lambda x: not &#39;optional&#39; in x[&#39;attributes&#39;], inargs)</span>
            <span class="c1">#opt_inargs = filter(lambda x: &#39;optional&#39; in x[&#39;attributes&#39;], inargs)</span>

            <span class="c1"># Check number of arguments is compatible</span>
            <span class="c1"># Should be in range len(oblig_inargs) &lt;= L &lt;= len(oblig_inargs) + len(opt_inargs)</span>
            <span class="k">if</span> <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">&lt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">oblig_inargs</span><span class="p">)</span> <span class="ow">or</span>
                <span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="n">kwargs</span><span class="p">)</span> <span class="o">&gt;</span> <span class="nb">len</span><span class="p">(</span><span class="n">oblig_inargs</span><span class="p">)</span> <span class="o">+</span> <span class="nb">len</span><span class="p">(</span><span class="n">opt_inargs</span><span class="p">)):</span>
                <span class="k">if</span> <span class="n">wraplog</span><span class="o">.</span><span class="n">getEffectiveLevel</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
                    <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Number of arguments incompatible: </span><span class="si">%d</span><span class="s1"> must be in range </span><span class="si">%d</span><span class="s1"> &lt;= n &lt;= </span><span class="si">%d</span><span class="s1">&#39;</span> <span class="o">%</span>
                                  <span class="p">(</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">oblig_inargs</span><span class="p">),</span> <span class="nb">len</span><span class="p">(</span><span class="n">oblig_inargs</span><span class="p">)</span><span class="o">+</span><span class="nb">len</span><span class="p">(</span><span class="n">opt_inargs</span><span class="p">)))</span>
                <span class="k">continue</span>

            <span class="n">newinargs</span> <span class="o">=</span> <span class="p">(</span><span class="n">oblig_inargs</span> <span class="o">+</span> <span class="n">opt_inargs</span><span class="p">)[:</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">)]</span>

            <span class="c1"># Check types and dimensions are compatible</span>
            <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">([</span><span class="n">type_is_compatible</span><span class="p">(</span><span class="n">spec</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">spec</span><span class="p">,</span><span class="n">a</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">newinargs</span><span class="p">,</span> <span class="n">args</span><span class="p">)]):</span>
                <span class="k">if</span> <span class="n">wraplog</span><span class="o">.</span><span class="n">getEffectiveLevel</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
                    <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Types and dimensions of args incompatible </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span>
                                  <span class="p">(</span><span class="n">newinargs</span><span class="p">,</span> <span class="n">args</span><span class="p">,</span> <span class="p">[</span><span class="n">type_is_compatible</span><span class="p">(</span><span class="n">spec</span><span class="p">,</span> <span class="n">a</span><span class="p">)</span> <span class="k">for</span> <span class="p">(</span><span class="n">spec</span><span class="p">,</span><span class="n">a</span><span class="p">)</span> <span class="ow">in</span> <span class="nb">zip</span><span class="p">(</span><span class="n">newinargs</span><span class="p">,</span> <span class="n">args</span><span class="p">)]))</span>
                <span class="k">continue</span>

            <span class="c1"># Check keyword arguments, if incompatible continue to next</span>
            <span class="c1"># candidate interface</span>
            <span class="k">if</span> <span class="n">kwargs</span><span class="p">:</span>
                <span class="n">innames</span> <span class="o">=</span> <span class="p">[</span><span class="n">badnames</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">],</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">oblig_inargs</span> <span class="o">+</span> <span class="n">opt_inargs</span><span class="p">]</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="nb">all</span><span class="p">([</span><span class="n">prefix</span><span class="o">+</span><span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="ow">in</span> <span class="n">innames</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">):]</span> <span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">keys</span><span class="p">()]):</span>
                <span class="k">if</span> <span class="n">wraplog</span><span class="o">.</span><span class="n">getEffectiveLevel</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
                    <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Unexpected keyword argument valid=</span><span class="si">%s</span><span class="s1"> got=</span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">kwargs</span><span class="o">.</span><span class="n">keys</span><span class="p">(),</span> <span class="n">innames</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">args</span><span class="p">):]))</span>
                <span class="k">continue</span>

            <span class="k">try</span><span class="p">:</span>
                <span class="k">for</span> <span class="n">key</span><span class="p">,</span> <span class="n">arg</span> <span class="ow">in</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">iteritems</span><span class="p">():</span>
                    <span class="p">(</span><span class="n">inarg</span><span class="p">,)</span> <span class="o">=</span> <span class="p">[</span><span class="n">x</span> <span class="k">for</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">inargs</span> <span class="k">if</span> <span class="n">badnames</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">],</span><span class="n">x</span><span class="p">[</span><span class="s1">&#39;name&#39;</span><span class="p">])</span><span class="o">.</span><span class="n">lower</span><span class="p">()</span> <span class="o">==</span> <span class="n">prefix</span><span class="o">+</span><span class="n">key</span><span class="o">.</span><span class="n">lower</span><span class="p">()]</span>
                    <span class="k">if</span> <span class="ow">not</span> <span class="n">type_is_compatible</span><span class="p">(</span><span class="n">inarg</span><span class="p">,</span> <span class="n">arg</span><span class="p">):</span>
                        <span class="k">if</span> <span class="n">wraplog</span><span class="o">.</span><span class="n">getEffectiveLevel</span><span class="p">()</span> <span class="o">&lt;=</span> <span class="n">logging</span><span class="o">.</span><span class="n">DEBUG</span><span class="p">:</span>
                            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Types and dimensions of kwarg </span><span class="si">%s</span><span class="s1"> incompatible&#39;</span> <span class="o">%</span> <span class="n">key</span><span class="p">)</span>
                        <span class="k">raise</span> <span class="ne">ValueError</span>
            <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
                <span class="k">continue</span>

            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;calling &#39;</span><span class="o">+</span><span class="n">rname</span><span class="p">)</span>
            <span class="k">return</span> <span class="n">routine</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>

        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;No matching routine found in interface </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>


    <span class="k">def</span> <span class="nf">func_simple</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
        <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Interface </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>

        <span class="k">for</span> <span class="n">rname</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">routine</span><span class="p">,</span> <span class="n">modfile</span> <span class="ow">in</span> <span class="n">routines</span><span class="p">:</span>
            <span class="n">wraplog</span><span class="o">.</span><span class="n">debug</span><span class="p">(</span><span class="s1">&#39;Trying candidate routine </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">rname</span><span class="p">)</span>
            <span class="k">try</span><span class="p">:</span>
                <span class="k">return</span> <span class="n">routine</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
            <span class="k">except</span> <span class="ne">Exception</span><span class="p">,</span> <span class="n">e</span><span class="p">:</span>
                <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="ne">RuntimeError</span><span class="p">):</span>
                    <span class="k">raise</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="k">continue</span>

        <span class="k">raise</span> <span class="ne">TypeError</span><span class="p">(</span><span class="s1">&#39;No matching routine found in interface </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span><span class="p">)</span>


    <span class="n">doc</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="n">intf_spec</span><span class="p">[</span><span class="s1">&#39;doc&#39;</span><span class="p">])</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span>

    <span class="n">doc</span> <span class="o">+=</span> <span class="s1">&#39;Routine is wrapper around Fortran interface ``</span><span class="si">%s</span><span class="s1">`` containing multiple routines:</span><span class="se">\n\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">name</span>

    <span class="k">for</span> <span class="n">rname</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">routine</span><span class="p">,</span> <span class="n">modfile</span> <span class="ow">in</span> <span class="n">routines</span><span class="p">:</span>

        <span class="c1"># regenerate routine documentation, using sphinx format rather than numpydoc format</span>
        <span class="n">tmp_routine</span> <span class="o">=</span> <span class="n">add_doc</span><span class="p">(</span><span class="n">routine</span><span class="p">,</span> <span class="n">routine</span><span class="o">.</span><span class="n">_fobj</span><span class="p">,</span> <span class="n">spec</span><span class="p">,</span> <span class="n">rname</span><span class="p">,</span> <span class="n">name</span><span class="p">,</span> <span class="n">prefix</span><span class="p">,</span> <span class="nb">format</span><span class="o">=</span><span class="s1">&#39;sphinx&#39;</span><span class="p">,</span> <span class="n">modfile</span><span class="o">=</span><span class="n">modfile</span><span class="p">)</span>


        <span class="n">routine_lines</span> <span class="o">=</span> <span class="n">tmp_routine</span><span class="o">.</span><span class="vm">__doc__</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>

        <span class="n">doc</span> <span class="o">+=</span>        <span class="p">(</span><span class="s1">&#39;  .. function :: </span><span class="si">%s</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">%</span> <span class="n">routine_lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">+</span>
            <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="s1">&#39;     </span><span class="si">%s</span><span class="s1">&#39;</span>   <span class="o">%</span> <span class="n">line</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">routine_lines</span><span class="p">[</span><span class="mi">1</span><span class="p">:]]))</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>

        <span class="c1">#doc = doc.replace(&quot;``%s``&quot;%rname,&quot;``%s``&quot;%rname.upper())</span>
        <span class="c1">#doc = doc.replace(rname,name)</span>
        <span class="c1">#doc = doc.replace(&quot;``%s``&quot;%rname.upper(),&quot;``%s``&quot;%rname)</span>

    <span class="n">func</span><span class="o">.</span><span class="vm">__doc__</span> <span class="o">=</span> <span class="n">doc</span>
    <span class="k">return</span> <span class="n">func</span>


<span class="k">def</span> <span class="nf">update_doc_string</span><span class="p">(</span><span class="n">doc</span><span class="p">,</span> <span class="n">extra</span><span class="p">,</span> <span class="n">sections</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">signature</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Insert `extra` in the docstring `doc`, before the first matching section</span>

<span class="sd">    Searches for each section heading in the list `sections` in turn.</span>
<span class="sd">    If sections is not given, the default is `[&#39;Parameters&#39;, &#39;See also&#39;]`.</span>
<span class="sd">    If not sections are found, extra text is appended to end of docstring.</span>

<span class="sd">    Returns the new doc string, suitable for assigning to cls.__doc__</span>
<span class="sd">    &quot;&quot;&quot;</span>

    <span class="k">if</span> <span class="n">sections</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
       <span class="n">sections</span> <span class="o">=</span> <span class="p">[</span><span class="s1">&#39;Parameters&#39;</span><span class="p">,</span> <span class="s1">&#39;See also&#39;</span><span class="p">]</span>

    <span class="k">try</span><span class="p">:</span>
       <span class="n">doc</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">cleandoc</span><span class="p">(</span><span class="n">doc</span><span class="p">)</span>
       <span class="n">extra</span> <span class="o">=</span> <span class="n">inspect</span><span class="o">.</span><span class="n">cleandoc</span><span class="p">(</span><span class="n">extra</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
       <span class="k">pass</span>

    <span class="n">extra</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span> <span class="o">+</span> <span class="n">extra</span> <span class="o">+</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span>

    <span class="n">lines</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span>

    <span class="k">if</span> <span class="n">signature</span> <span class="ow">is</span> <span class="ow">not</span> <span class="kc">None</span><span class="p">:</span>
        <span class="n">lines</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="n">signature</span>

    <span class="k">for</span> <span class="n">section</span> <span class="ow">in</span> <span class="n">sections</span><span class="p">:</span>
       <span class="n">indices</span> <span class="o">=</span> <span class="p">[</span><span class="n">i</span> <span class="k">for</span> <span class="n">i</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span> <span class="k">if</span> <span class="n">line</span> <span class="o">==</span> <span class="n">section</span> <span class="p">]</span>
       <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">indices</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
          <span class="k">break</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="n">indices</span> <span class="o">=</span> <span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">lines</span><span class="p">)</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="c1"># insert at end</span>

    <span class="n">index</span><span class="p">,</span> <span class="o">=</span> <span class="n">indices</span>
    <span class="n">doc</span> <span class="o">=</span> <span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">line</span><span class="o">.</span><span class="n">rstrip</span><span class="p">()</span> <span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span><span class="p">[:</span><span class="n">index</span><span class="p">]</span> <span class="o">+</span> <span class="n">extra</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n</span><span class="s1">&#39;</span><span class="p">)</span> <span class="o">+</span> <span class="n">lines</span><span class="p">[</span><span class="n">index</span><span class="p">:]])</span>
    <span class="n">doc</span> <span class="o">=</span> <span class="n">doc</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="s1">&#39;</span><span class="se">\n\n\n</span><span class="s1">&#39;</span><span class="p">,</span> <span class="s1">&#39;</span><span class="se">\n\n</span><span class="s1">&#39;</span><span class="p">)</span>

    <span class="k">return</span> <span class="n">doc</span>
</pre></div>

           </div>
           
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2008-2016, James Kermode

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.Navigation.enable(true);
      });
  </script>

  
  
    
   

</body>
</html>